During the design of the program, Chinese characters may occur when the record set is stored, data is transmitted over the network, and data in the resource file is read.
The essence of Chinese problems is that the character encoding used for saving and transmitting Chinese characters is different from the character encoding used for reading and obtaining Chinese characters. All the phones in j2s support character set in UTF-8 format.
When data is used, the problem of Chinese characters is usually generated when the string and byte array are converted. The following is a conversion method for encoding Chinese characters to produce garbled characters:
Import java. io .*;
Public class Test {
/**
* Convert a byte array to a string
* @ Param bytes the byte array to be converted
* @ Return refers to the converted string.
*/
Public static String byte2String (byte [] bytes ){
Try {
ByteArrayInputStream bais = new ByteArrayInputStream (bytes );
DataInputStream dis = new DataInputStream (bais );
String s = dis. readUTF ();
// Close the stream
Dis. close ();
Bais. close ();
Return s;
} Catch (Exception e ){
Return null;
}