Home > Java > Javagetting Started > What is the reason for garbled reading and writing of IO streams in Java?

What is the reason for garbled reading and writing of IO streams in Java?

青灯夜游
Release: 2019-12-31 16:55:11
Original
2282 people have browsed it

What is the reason for garbled reading and writing of IO streams in Java?

Why may garbled characters occur when reading and writing IO streams? ? what is the reason?

1. The encoding and decoding rules are inconsistent

2. This character encoding does not support a certain language (ex: Chinese)

What should we do? How to avoid garbled characters as much as possible? ?

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

There are corresponding methods in String:

①: byte[] getBytes(): Encode this String into a byte sequence using the platform's default character set

②: byte[] getBytes(Charset charset): 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 is a corresponding construction method 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 the specified byte array

Recommended learning:Java video Tutorial

The above is the detailed content of What is the reason for garbled reading and writing of 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template