Java设置编码读写文件笔记

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class Test {
        public static void main(String[] args) {
            try{
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(new FileInputStream(url),"UTF-8"));
                String data = null;
                StringBuffer str=new StringBuffer();
                while((data = br.readLine())!=null)
                {
                    str.append(data+"\n");
                }
                System.out.println(str);
                br.close();
                Test test=new Test();
                test.ece(str.toString());
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        public void ece(String str){
            try{
                BufferedWriter bw = new BufferedWriter(
                        new OutputStreamWriter(new FileOutputStream(url,false),"UTF-8"));
                bw.write(str);
                bw.flush();
                bw.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }
}

Comments