[java][nio]Buffer的get, put, get(index), put(index)


import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class UsingBuffers {

	
	public static void main(String[] args) {
		char[] data = "UsingBuffers".toCharArray();
		ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
		CharBuffer cb = bb.asCharBuffer();
		cb.put(data);
		cb.rewind();
		System.out.println(cb.get() + "  " + cb.position());//U 1
		System.out.println(cb.get(3) + "  " + cb.position());//n 1
		System.out.println(cb.limit());//12
		cb.put(5,'x');
		System.out.println(cb.limit() + "  " + cb.position());//12 1
		cb.put('z');
		System.out.println(cb.limit() + "  " + cb.position());//12 2

	}


}


调用Buffer的相对的get()/put(),position指针会随之发生变化。
调用Buffer的绝对的get(index)/put(index), position指针不会改变。



你可能感兴趣的:(java)