Java ANSI转码UTF-8

 1 public static void change(String filepath) throws UnsupportedEncodingException, IOException{
 2     BufferedReader buf = null;
 3     OutputStreamWriter pw=null;        
 4     String str = null;
 5     String allstr="";
 6         
 7     //用于输入换行符的字节码
 8     byte[] c=new byte[2];
 9         c[0]=0x0d;
10         c[1]=0x0a;
11         String t=new String(c);
12         
13         buf=new BufferedReader(new InputStreamReader(new FileInputStream(filepath), "GBK"));
14         while((str = buf.readLine()) != null){
15             allstr=allstr+str+t;
16         }
17         
18         buf.close();        
19 
20         pw =new OutputStreamWriter(new FileOutputStream(filepath),"UTF-8");
21         pw.write(allstr); 
22         pw.close();
23     }

  尝试用了StringBuffer进行内容的存储,结果乱码,原因待查。最后使用String相加的方式解决。

posted @ 2013-12-19 16:37  踏刃而起  阅读(5117)  评论(0编辑  收藏  举报