package cn.bunny.file_dir;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class Demo5 {

    public static void main(String[] args) {
        // 将字符串转成ByteBuffer
        ByteBuffer buffer = ByteBuffer.allocate(16);
        buffer.put("hello".getBytes());

        // 转换方式二
        ByteBuffer buffer1 = StandardCharsets.UTF_8.encode("hello");
        System.out.println(buffer1);

        // 转换方式三
        ByteBuffer buffer2 = ByteBuffer.wrap("hello".getBytes());
        System.out.println(buffer2);

        // 使用读模式才能有值
        buffer1.flip();
        String str1 = StandardCharsets.UTF_8.decode(buffer1).toString();
        System.out.println(str1);
    }
}
