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!