首页  

Java NIO Scatter Gather     所属分类 nio 浏览量 941
分散 (scatter)
把 Channel 中的数据依次写入到多个 Buffer 

聚集 (gather)
将多个 Buffer 中的数据依次写入到 Channel 中

String home = System.getProperty("user.home");
System.out.println("home="+home);

ByteBuffer buf1 = ByteBuffer.wrap("hello".getBytes());
ByteBuffer buf2 = ByteBuffer.wrap("java".getBytes());
ByteBuffer buf3 = ByteBuffer.wrap("NIO".getBytes());
		
ByteBuffer[]bufs = new ByteBuffer[] {buf1,buf2,buf3};
		
String file = home+"/GatherAndScatter.txt";
FileChannel fileChannel = FileChannel.open(Paths.get(file),StandardOpenOption.CREATE,StandardOpenOption.WRITE);		

fileChannel.write(bufs);
fileChannel.close();
		
fileChannel = FileChannel.open(Paths.get(file));		
System.out.println("file size "+fileChannel.size());		
clear(bufs);
fileChannel.read(bufs);	
print(bufs);
fileChannel.close();


完整代码 https://gitee.com/dyyx/hellocode/blob/master/src/nio/GatherAndScatterTest.java

上一篇     下一篇
java NIO buffer

Java NIO pipe

java NIO FileChannel

java NIO SocketChannel

Java NIO 内存映射文件

java随机数生成器