public static void main(String[] args) {// 1. 字符串转为 ByteBufferByteBuffer buffer1 = ByteBuffer.allocate(16);buffer1.put("hello".getBytes());//仍然处于写模式// 2. CharsetByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");//操作完后处于读模式// 3. wrapByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());// 4. 转为字符串String str1 = StandardCharsets.UTF_8.decode(buffer2).toString();System.out.println(str1);buffer1.flip();String str2 = StandardCharsets.UTF_8.decode(buffer1).toString();System.out.println(str2);}
