用 java 将文件的编码从GBK 转换成 UTF8

时间:2022-04-01 14:05:59

http://blog.csdn.net/laorer/archive/2009/02/08/3868962.aspx

 

非常好,解决了我的一个大问题。真是感谢。代码生成的java和jsp终于不再手工转码了。

 

核心代码:

private static void transferFile(String srcFileName, String destFileName) throws IOException {
  String line_separator = System.getProperty("line.separator");
  FileInputStream fis = new FileInputStream(srcFileName);
  StringBuffer content = new StringBuffer();
  DataInputStream in = new DataInputStream(fis);
  BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));// , "UTF-8" 
  String line = null;
  while ((line = d.readLine()) != null)
   content.append(line + line_separator);
  d.close();
  in.close();
  fis.close();
     
  Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), "utf-8");
  ow.write(content.toString());
  ow.close();
 }

 

将一个GBK编码的文件转成UTF8 编码的文件


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/laorer/archive/2009/02/08/3868962.aspx