提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用asCharBuffer视图创建ByteBuffer的char视图
import java.nio.Buffer; import java.nio.ByteBuffer; import java.nio.ByteOrder; /** from 时 代 J a v a - nowjava.com**/ import java.nio.CharBuffer; public class Main { public static void main(String[] argv) throws Exception { ByteBuffer byteBuffer = ByteBuffer.allocate(7).order(ByteOrder.BIG_ENDIAN); CharBuffer charBuffer = byteBuffer.asCharBuffer(); byteBuffer.put(0, (byte) 0); byteBuffer.put(1, (byte) 'H'); byteBuffer.put(2, (byte) 0);//NowJava.com - 时 代 Java byteBuffer.put(3, (byte) 'i'); byteBuffer.put(4, (byte) 0); byteBuffer.put(5, (byte) '!'); byteBuffer.put(6, (byte) 0); println(byteBuffer); println(charBuffer); } // Print info about a buffer private static void println(Buffer buffer) { System.out.println