Java NIO -與IO的區別 -NIO核心 -Buffer核心屬性 -直接與非直接緩衝區 -通道Channel -分散與聚集

NIO和IO的區別

NIO的核心

 

Buffer

1.分配緩衝區

2.PUT數據到緩衝區

緩衝區的核心屬性

圖示

 

讀數據模式(position和limit會變)

代碼

public class TestBuffer {
		
	@Test
	public void test1() {
		String s = "abcd";
		
		ByteBuffer buf = ByteBuffer.allocate(1024);
		System.out.println("allocate() : ");
		System.out.println(buf.position());
		System.out.println(buf.limit());
		System.out.println(buf.capacity());
		System.out.println();
		
		buf.put(s.getBytes());
		System.out.println(buf.position());
		System.out.println();
		
		buf.flip();
		System.out.println(buf.position());
		System.out.println(buf.limit());
		byte[] dst = new byte[buf.limit()];
		buf.get(dst);
		System.out.println(new String(dst, 0, dst.length));
		System.out.println(buf.position());
		
		
		buf.clear();
		System.out.println((char)buf.get());
		
//		buf.rewind(); 可重複讀		
		
		
	}
}

mark(position可以reset到mark的位置)

直接與非直接緩衝區

通道

複製文件(非直接)

通過映射的方法(略)

通道之間的傳輸(直接緩衝區)

分散與聚集

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章