我有一个带有汉字文本的文件,我想将这些文本复制到另一个文件中。但是文件输出混乱了中文字符。注意,在我的代码中,我已经在使用“ UTF8”作为编码了:
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();
在这种情况下,请勿使用FileReader,因为它不允许您指定输入编码。在FileInputStream上构造一个InputStreamReader。
像这样:
BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(inputXml), "UTF8"));