What to do if the dataoutputstream is garbled?
Solution to garbled dataoutputstream: 1. Write String through "dos.write("...".getBytes());"; 2. Set "new OutputStreamWriter(new FileOutputStream(file), "utf-8");" is enough.
#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)
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());
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); }
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();
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to garbled word page numbers: 1. Open the word document and click the "File" option in the upper left corner; 2. Select the "More" option, and then click the "Options" button; 3. Select "Advanced" in the word options; 4. . Find "Show field codes instead of field values" in "Show document content", remove the check in front, and click OK to return to the home page.

The Linux Chinese garbled problem is a common problem when using Chinese character sets and encodings. Garbled characters may be caused by incorrect file encoding settings, system locale not being installed or set, and terminal display configuration errors, etc. This article will introduce several common workarounds and provide specific code examples. 1. Check the file encoding setting. Use the file command to view the file encoding. Use the file command in the terminal to view the encoding of the file: file-ifilename. If there is "charset" in the output

Solutions to garbled tomcat startup: 1. Modify Tomcat's conf configuration file; 2. Modify the system language; 3. Modify the command line window encoding; 4. Check the Tomcat server configuration; 5. Check the project encoding; 6. Check the log file; 7 , try other solutions. Detailed introduction: 1. Modify Tomcat's conf configuration file, open Tomcat's conf directory, find the "logging.properties" file, etc.

Solution to Chinese garbled characters in Linux tty: 1. Download the font fbterm through the "sudo apt-get install fbterm" command; 2. Execute the "sudo fbterm" command; 3. Change the font and font size to "font-names=Ubuntu Mono font- size=14” is enough.

In the Windows 10 system, garbled characters are common. The reason behind this is often that the operating system does not provide default support for some character sets, or there is an error in the set character set options. In order to prescribe the right medicine, we will analyze the actual operating procedures in detail below. How to solve Windows 10 garbled code 1. Open settings and find "Time and Language" 2. Then find "Language" 3. Find "Manage Language Settings" 4. Click "Change System Regional Settings" here 5. Check the box as shown and click Just make sure.

How to solve the problem of garbled text documents in win11? When many users use the win11 system, text documents are garbled and cannot be read normally. Many friends do not know how to solve this problem. In fact, this method is not difficult. Below, the editor has compiled the steps to solve the problem of garbled Windows 11 system documents. I hope it can bring you some inspiration! Steps to solve garbled Windows 11 system documents: 1. First, open the control panel of win11, enter control panel in the search box below, and click Search to enter the control panel. 2. After entering the panel, find the clock and area and click to enter, then click on the area option. 3. After entering, click on the management panel, and then click on Change system regional settings.

When many users use computers, they will find that there are many files with the suffix dll, but many users do not know how to open such files. For those who want to know, please take a look at the following details. Tutorial~How to open and edit dll files: 1. Download a software called "exescope" and download and install it. 2. Then right-click the dll file and select "Edit resources with exescope". 3. Then click "OK" in the pop-up error prompt box. 4. Then on the right panel, click the "+" sign in front of each group to view the content it contains. 5. Click on the dll file you want to view, then click "File" and select "Export". 6. Then you can

Some friends want to open a notepad and find that their win11 notepad is garbled and don't know what to do. In fact, we generally only need to modify the region and language. Win11 Notepad is garbled: First step, use the search function, search and open "Control Panel" Second step, click "Change date, time or number format" under Clock and Region Third step, click the "Manage" option above Card. The fourth step is to click "Change System Regional Settings" below. The fifth step is to change the current system regional settings to "Chinese (Simplified, China)" and click "OK" to save.
