Write chinese characters from one file to another

谁都会走 提交于 2019-12-02 09:07:46

问题


I have a file with Chinese characters text inside, I want to copy those text over to another file. But the file output messes with the chinese characters. Notice that in my code I am using "UTF8" as my encoding already:

BufferedReader br = new BufferedReader(new FileReader(inputXml));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
String everythingUpdate = sb.toString();

Writer out = new BufferedWriter(new OutputStreamWriter(
        new FileOutputStream(outputXml), "UTF8"));

out.write("");
out.write(everythingUpdate);
out.flush();
out.close();

回答1:


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"));



回答2:


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();
}


来源:https://stackoverflow.com/questions/15377756/write-chinese-characters-from-one-file-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!