public String test(String inPath,String outPath) throws Exception {
        // 以GBK格式读取文件
        FileInputStream fis = new FileInputStream(inPath);
        InputStreamReader isr = new InputStreamReader(fis, "GBK");
        BufferedReader br = new BufferedReader(isr);
        String str = null;
        // 创建StringBuffer字符串缓存区
        StringBuffer sb = new StringBuffer();
        // 通过readLine()方法遍历读取文件
        while ((str = br.readLine()) != null) {
            // 使用readLine()方法无法进行换行,需要手动在原本输出的字符串后面加"\n"或"\r"
            str += "\n";
            sb.append(str);
        }
        String str2 = sb.toString();// 以UTF-8文件写入
        FileOutputStream fos = new FileOutputStream(outPath, false);
        OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
        osw.write(str2);
        osw.flush();
        osw.close();
        fos.close();
        br.close();
        isr.close();
        fis.close();
        return "文件生成成功";
    }

 

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2021-12-10
  • 2021-09-26
  • 2021-08-06
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2020-03-15
  • 2021-09-14
  • 2021-11-07
  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
相关资源
相似解决方案