简体   繁体   English

如何在 utf-8 中读写这个?

[英]How to read write this in utf-8?

I was getting an error io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence我收到一个错误io.MalformedByteSequenceException: Invalid byte 2 of 2-byte UTF-8 sequence

The solution is to read and write file in UTF-8.解决方案是以 UTF-8 读写文件。

My code is:我的代码是:

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

I'm reading a url and writing it to a file DirectionResponse.xml .我正在读取一个 url 并将其写入文件DirectionResponse.xml Then reading DirectionResponse.xml and writing the same as *ppre_DirecionResponse.xml* for processing.然后读取DirectionResponse.xml并写入与 *ppre_DirecionResponse.xml* 相同的内容进行处理。

How do I change this so that reading and writing is done in UTF-8?如何更改它以便以 UTF-8 进行读写?

First, you need to call output.close() (or at least call output.flush() ) before you reopen the file for input.首先,您需要在重新打开文件进行输入之前调用output.close() (或至少调用output.flush() )。 That's probably the main cause of your problems.这可能是你的问题的主要原因。

Then, you shouldn't use FileReader or FileWriter for this because it always uses the platform-default encoding (which is often not UTF-8).然后,您不应为此使用FileReaderFileWriter ,因为它始终使用平台默认编码(通常不是 UTF-8)。 From the docs for FileReader :来自FileReader的文档

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate.此类的构造函数假定默认字符编码和默认字节缓冲区大小是适当的。

You have the same problem when using a FileWriter .使用FileWriter时遇到同样的问题。 Replace this:替换这个:

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

with something like this:像这样:

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

and similarly for fstream .对于fstream也是如此。

Read and Write UTF-8 File in Java 用 Java 读写 UTF-8 文件

I see you are writing in utf-8 but not specifically reading in utf-8.我看到你正在用 utf-8 写作,但没有专门用 utf-8 阅读。 Follow the example I've provided in the link.按照我在链接中提供的示例进行操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM