日期:2014-05-20  浏览次数:21462 次

将文件为UTF-8格式转化为ANSI格式,仍然乱码
现在有一需求,有一文件是utf-8编码格式其内容有乱码,如果我将此文件用 文本编辑器 打开并另存为且以ANSI格式保存后,其乱码变为正确的中文。但是我用java写了一个程序转换(即以utf-8读取,以gb2312格式写入另一文件),新的文件还是乱码!
  想请高手指点指点,谢谢
   
  我的程序是参考 http://dali.iteye.com/blog/73860 文章!

------解决方案--------------------

Java code

public static void convertUTFFileToGBKFile(String srcFileName, String destFileName) {//把文件转换为GBK文件
        BufferedReader br = null;
        ;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(
                    srcFileName), "utf-8"));
            bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(destFileName), "gbk"));
            String line = null;
            while ((line = br.readLine()) != null) {
                bw.write(line);
                bw.newLine();
            }
        } catch (Exception e) {
        } finally {
            try {
                if (br != null)
                    br.close();
                if (bw != null)
                    bw.close();
            } catch (IOException e) {
            }
        }
    }