Notes: I/O Stream-Character Set, Character Set
Java. nio uses the Charset class to unify the conversion of character sets. Payment Jie establishes a ing between the two-byte Unicode code sequence and the byte sequence using the local character encoding method, the Charset class uses the character set names standardized by the IANA Character Set registration center. To be compatible with other naming conventions, each character set can have many aliases, you can call the aliases method to return the Set object composed of aliases. The sample code is as follows:
Charset charset = Charset. forName ("UTF-8 ");
System. out. println ("UTF-8 charset ");
Set <String> aliases = charset. aliases ();
For (String a: aliases ){
System. out. println ("aliase is" + );
}
To determine which character sets are available in a specific implementation, you can call the static avaliableCharsets method. The sample code is as follows:
Map <String, Charset> charsetMap = Charset. availableCharsets ();
For (String c: charsetMap. keySet ()){
System. out. println (c );
}
If a character cannot be represented by a character set, it is converted ?, Once a character set is available, it can be converted between a Java string containing Unicode code elements and a encoded byte sequence. The following code is used to encode a Java string:
String str = "received legal framework dsfjkl23 ";
// Encode the string to the Byte array
ByteBuffer buffer = charset. encode (str );
Byte [] bytes = buffer. array ();
For (byte B: bytes ){
System. out. print (Integer. toHexString (B &0xff) + "");
}
System. out. println ();
// Decodes the Byte array into a string
ByteBuffer decodeBuffer = ByteBuffer. wrap (bytes );
CharBuffer charBuffer = charset. decode (decodeBuffer );
System. out. println (charBuffer. toString ());