Write chinese characters from one file to another

倾然丶 夕夏残阳落幕 提交于 2019-12-02 04:44:56

You should not use FileReader in a case like this, as it does not let you specify input encoding. Construct an InputStreamReader on a FileInputStream.

Something like this:

BufferedReader br = 
        new BufferedReader(
            new InputStreamReader(
                new FileInputStream(inputXml), 
                "UTF8"));

The answer from @hyde is valid, but I have two extra notes that I will point out in the code below.

Of course it is up to you to re-organize the code to your needs

// Try with resource is used here to guarantee that the IO resources are properly closed
// Your code does not do that properly, the input part is not closed at all
// the output an in case of an exception, will not be closed as well
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputXML), "UTF-8"));
    PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputXML), "UTF8"))) {
    String line = reader.readLine();

    while (line != null) {
    out.println("");
    out.println(line);

    // It is highly recommended to use the line separator and other such
    // properties according to your host, so using System.getProperty("line.separator")
    // will guarantee that you are using the proper line separator for your host
    out.println(System.getProperty("line.separator"));
    line = reader.readLine();
    }
} catch (IOException e) {
  e.printStackTrace();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!