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!