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的位置)

直接与非直接缓冲区

通道

复制文件(非直接)

通过映射的方法(略)

通道之间的传输(直接缓冲区)

分散与聚集

 

 

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