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