我收到了错误io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence
解决方案是以UTF-8读写文件。
我的代码是:
InputStream input = null;
OutputStream output = null;
OutputStreamWriter bufferedWriter = new OutputStreamWriter( output, "UTF8");
input = new URL(url).openStream();
output = new FileOutputStream("DirectionResponse.xml");
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
FileWriter fstream = new FileWriter("ppre_DirectionResponse.xml");
BufferedWriter out = new BufferedWriter(fstream);
我正在阅读网址并将其写入文件 DirectionResponse.xml 。然后读取 DirectionResponse.xml 并将其写为* ppre_DirecionResponse.xml *进行处理。
如何更改此选项以便以UTF-8完成阅读和写作?
答案 0 :(得分:26)
首先,您需要在重新打开文件输入之前调用output.close()
(或至少调用output.flush()
。这可能是导致问题的主要原因。
然后,您不应该使用FileReader
或FileWriter
,因为它始终使用平台默认编码(通常不是UTF-8)。来自the docs for FileReader
:
此类的构造函数假定默认字符编码和默认字节缓冲区大小是合适的。
使用FileWriter
时遇到同样的问题。替换这个:
BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));
有这样的事情:
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream("DirectionResponse.xml"), "UTF-8"));
,同样适用于fstream
。
答案 1 :(得分:2)
Read and Write UTF-8 File in Java
我看到你是用utf-8写的,但没有专门用utf-8阅读。按照我在链接中提供的示例。
try {
Reader reader =
new InputStreamReader(
new FileInputStream(args[0]),"UTF-8");
BufferedReader fin = new BufferedReader(reader);
Writer writer =
new OutputStreamWriter(
new FileOutputStream(args[1]), "UTF-8");
BufferedWriter fout = new BufferedWriter(writer);
String s;
while ((s=fin.readLine())!=null) {
fout.write(s);
fout.newLine();
}
//Remember to call close.
//calling close on a BufferedReader/BufferedWriter
// will automatically call close on its underlying stream
fin.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}