Home >Java >JavaBase >Causes and avoidance methods of garbled IO streams in Java

Causes and avoidance methods of garbled IO streams in Java

尚
Original
2019-12-25 16:58:182163browse

Causes and avoidance methods of garbled IO streams in Java

The reason why garbled characters may appear during IO stream reading and writing:

①Encoding and decoding rules are inconsistent

②This character encoding does not support a certain Language (ex: Chinese)

Methods to avoid garbled characters:

1. Encoding: String ---> byte[]

There is a corresponding in String Methods:

①: byte[] getBytes(): Use the platform’s default character set to encode this String into a byte sequence

②: byte[] getBytes(Charset charset): Use the specified Use the specified character encoding to encode the string

③: byte[] getBytes(String charsetName): Use the specified character encoding to encode the string

2. Decoding: byte[] ---> ; String

There are corresponding construction methods in String:

①:String(byte[] bytes): Decode the specified byte array by using the platform’s default character set

②: String(byte[] bytes, Charset charset): Use the specified character set to decode the specified byte array

③: String(byte[] bytes, String charsetName): Use the specified character set to decode Specified byte array

For more java knowledge, please pay attention to the java basic tutorial column.

The above is the detailed content of Causes and avoidance methods of garbled IO streams in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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