如何阅读用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 完成读取和写入?


答案 1

首先,您需要在重新打开文件进行输入之前调用(或至少调用)。这可能是您问题的主要原因。output.close()output.flush()

然后,您不应该为此使用 或,因为它始终使用平台默认编码(通常不是 UTF-8)。从 FileReader 的文档:FileReaderFileWriter

此类的构造函数假定默认字符编码和默认字节缓冲区大小是合适的。

使用 .替换此:FileWriter

BufferedReader br = new BufferedReader(new FileReader("DirectionResponse.xml" ));

用这样的东西:

BufferedReader br = new BufferedReader(new InputStreamReader(
    new FileInputStream("DirectionResponse.xml"), "UTF-8"));

同样,对于 .fstream


答案 2

在 Java 中读取和写入 UTF-8 文件

我看到你正在用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();
}

推荐