java--解决文件输出流保存内容无中文时文件编码为ANSI格式不是utf-8

来源:互联网 发布:彩票查询软件 编辑:程序博客网 时间:2024/06/06 13:25

java使用流写出文件时,即使指定了utf-8文件格式,但是如果内容中没有中文,编码会自动改为ansi格式。


生成的代码如下:

public static void saveFile(String file, String data, boolean append) throws IOException {  
      BufferedWriter bw = null;  
      OutputStreamWriter osw = null;  
          
      File f = new File(file);  
      FileOutputStream fos = new FileOutputStream(f, append);  
      try {  
         // write UTF8 BOM mark if file is empty  
         if (f.length() < 1) {  
            final byte[] bom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF };  
            fos.write(bom);  
         }  
  
         osw = new OutputStreamWriter(fos, "UTF-8");  
         bw = new BufferedWriter(osw);  
         if (data != null) bw.write(data);  
      } catch (IOException ex) {  
         throw ex;  
      } finally {  
         try { bw.close(); fos.close(); } catch (Exception ex) { }  
      }  
   } 


另外,输出的utf8文件中中文是乱码,需要考虑2点,一是输出,是否指定了正确的输出格式。二是输入是否按照正确的文件格式去解析了文件。

比如,读一个utf8文件,然后写出另一个utf8文件


         File rst01 = new File("a.txt");
         File rst02 = new File("b.txt");

         InputStreamReader isr = new InputStreamReader(new FileInputStream(rst01), "UTF-8");
         BufferedReader reader = new BufferedReader(isr);
         OutputStreamWriter writerStream = new OutputStreamWriter(new FileOutputStream(rst02),"UTF-8");
         BufferedWriter writer = new BufferedWriter(writerStream);



阅读全文
0 0
原创粉丝点击