如何阅读用utf-8写这个?
2022-09-03 09:26:55
我收到错误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);
我正在读取一个url并将其写入文件DirectionResponse.xml。然后读取 DirectionResponse.xml并写入与 *ppre_DirecionResponse.xml* 相同的内容进行处理。
如何更改此设置,以便以 UTF-8 完成读取和写入?