1. public static void main(String[] args) {
    2. // 1. 字符串转为 ByteBuffer
    3. ByteBuffer buffer1 = ByteBuffer.allocate(16);
    4. buffer1.put("hello".getBytes());//仍然处于写模式
    5. // 2. Charset
    6. ByteBuffer buffer2 = StandardCharsets.UTF_8.encode("hello");//操作完后处于读模式
    7. // 3. wrap
    8. ByteBuffer buffer3 = ByteBuffer.wrap("hello".getBytes());
    9. // 4. 转为字符串
    10. String str1 = StandardCharsets.UTF_8.decode(buffer2).toString();
    11. System.out.println(str1);
    12. buffer1.flip();
    13. String str2 = StandardCharsets.UTF_8.decode(buffer1).toString();
    14. System.out.println(str2);
    15. }