//返回系统默认字符集
Charset defaultCharset = Charset.defaultCharset();
//返回UTF_8字符集
Charset charset = StandardCharsets.UTF_8;
//使用字符集创建编码器和解码器
CharsetEncoder encoder = charset.newEncoder();
CharsetDecoder decoder = charset.newDecoder();
//使用编码器和解码器解析数据
//略
Scanner in = new Scanner(System.in);
System.out.println("请输入数据:");
String str = in.nextLine();
System.out.println("原始数据:" + str);
ByteBuffer byteBuffer = charset.encode(str);
System.out.println("UTF-8编码:" + Arrays.toString(byteBuffer.array()));
CharBuffer charBuffer = charset.decode(byteBuffer);
System.out.println("解码后:" + charBuffer);