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相加的方式解決。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM