java io写文件 在linux出现中文乱码

   java io写文件时写到文件的中文都成乱码,在windows是没问题的,主要是linux下默认字符集是utf-8,而windows默认编码是gbk,所以java 往文件里面写入中文时是根据系统默认字符集来写的。可以有两种方法,

1.在java io往文件写时强制用系统编码写,

 String fileEncode = System.getProperty("file.encoding");
      File outFile = 
new
 File(path+separator+fileName);
      
if
(!outFile.exists()){
         outFile.createNewFile();
      }
      
else
{
         outFile.delete();
         outFile.createNewFile();
      }
      
      OutputStreamWriter ow=
new OutputStreamWriter(new
 FileOutputStream(outFile),fileEncode);
      
for(int
 i=0;i<failList.size();i++)
      {
         ow.write(
new String(failList.get(i).getBytes("UTF-8"),fileEncode)+"/r/n"
);
      }
      ow.close();

2.修改linux的系统编码字符集

 修改/etc/sysconfig/i18n 这个文件

  将LANG="zh_CN.UTF-8"修改为:
  LANG="zh_CN.GBK"
   保存并关闭,运行下面的命令使配置生效:
  shell> source /etc/sysconfig/i18n

原文地址:https://www.cnblogs.com/secbook/p/2655255.html