Home> Java> JavaBase> body text

Causes and avoidance methods of garbled IO streams in Java

Release: 2019-12-25 17:11:04
Original
2033 people have browsed it

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 thejava basic tutorialcolumn.

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!

Related labels:
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
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!