编码:字符串 -> 字节数组
解码:字节数组 -> 字符串
示例
查看Charset集合:
Charset.availableCharsets().entrySet().forEach(entry -> {
System.out.println(entry.getKey() + " = " + entry.getValue());
});
/*
Big5 = Big5
Big5-HKSCS = Big5-HKSCS
CESU-8 = CESU-8
EUC-JP = EUC-JP
EUC-KR = EUC-KR
....
x-windows-iso2022jp = x-windows-iso2022jp
*/
编码解码:
Charset charset = Charset.forName("UTF-8");
CharBuffer source = CharBuffer.allocate(1024);
source.put("包子");
// 编码
source.flip();
ByteBuffer byteBuffer = charset.encode(source);
// 解码
byteBuffer.flip();
CharBuffer charBuffer = charset.decode(byteBuffer);