Home > Java > JavaBase > body text

What to do if the dataoutputstream is garbled?

藏色散人
Release: 2023-01-05 11:28:53
Original
30621 people have browsed it

Solution to garbled dataoutputstream: 1. Write String through "dos.write("...".getBytes());"; 2. Set "new OutputStreamWriter(new FileOutputStream(file), "utf-8");" is enough.

What to do if the dataoutputstream is garbled?

#The operating environment of this tutorial: Windows 10 system, Java version 8.0, Dell G3 computer.

What should I do if the dataoutputstream is garbled?

Solve the problem of DataOutputStream garbled characters

I will step on this trap first, and say the important things three times!

Never use the writeBytes method of DataOutputStream

Never use the writeBytes method of DataOutputStream

千Never use the writeBytes method of DataOutputStream

When we use DataOutputStream, for example, if we want to write String, you will see three methods

public final void writeBytes(String s)
public final void writeChars(String s)
public final void writeUTF(String str)
Copy after login

OK , then you try to write the same content and then read it again

File file = new File("d:"+File.separator+"test.txt");
   DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
   dos.writeBytes("你好");
   dos.writeChars("你好");
   dos.writeUTF("你好");
   dos.flush();
   dos.close();
   
   DataInputStream dis = new DataInputStream(new FileInputStream(file));
   byte[] b = new byte[2];
   dis.read(b);
            //  `}
   System.out.println(new String(b, 0, 2));
   
   char[] c = new char[2];
   for (int i = 0; i < 2; i++) {
    c[i] = dis.readChar();
   }
            //你好
   System.out.println(new String(c, 0, 2));
   //你好
   System.out.println(dis.readUTF());
Copy after login

Yes, you read it right, the content written by the writeBytes method is read out, Why is it garbled?

Click in to see the implementation

public final void writeBytes(String s) throws IOException {
        int len = s.length();
        for (int i = 0 ; i < len ; i++) {
            out.write((byte)s.charAt(i));
        }
        incCount(len);
    }
Copy after login

Brother, this char type has been forcibly converted to byte type, and the accuracy has been lost. No wonder it cannot be returned, so use Don't be greedy for convenience, just replace it with dos.write("Hello".getBytes()); It's all good

DataOutputStream writes garbled data to the txt file

This is normal. If you want to read, use DataInputStream to read. If you only want to save it as a text file, directly use FileOutputStream or PrintWriter

OutputStreamWriter oStreamWriter = new OutputStreamWriter(new FileOutputStream(file), "utf-8");
oStreamWriter.append(str);
oStreamWriter.close();
Copy after login

The main reason is that the encoding method is different

Use a character stream instead of a byte stream

The BufferedReader class reads text from a character input stream and buffers the characters to effectively read characters, arrays and lines

Recommended learning: "Java Video Tutorial"

The above is the detailed content of What to do if the dataoutputstream is garbled?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!