NIO分散读取与聚集写入

来源:互联网 发布:澳洲股市行情软件app 编辑:程序博客网 时间:2024/05/18 21:42

分散(Scatter)和聚集(Gather)
分散读取(Scattering Reads): 将通道中的数据分散到多个缓冲区中
聚集写入(Gathering Writes): 将多个缓冲区中的数据聚集到通道中

    @Test  // 分散(Scatter)和聚集(Gather)    public void test7() throws IOException{        RandomAccessFile raf1=new RandomAccessFile("1.txt","rw");        //1、获取通道        FileChannel channel1=raf1.getChannel();        //2、分配指定大小的缓冲区        ByteBuffer buf1=ByteBuffer.allocate(100);        ByteBuffer buf2=ByteBuffer.allocate(200);        //3、分散读取        ByteBuffer[] bufs={buf1,buf2};        channel1.read(bufs);        for(ByteBuffer byteBuffer:bufs){            byteBuffer.flip();        }        System.out.println(new String(bufs[0].array(),0,bufs[0].limit()));        System.out.println("------------------");        System.out.println(new String(bufs[1].array(),0,bufs[1].limit()));    }
原创粉丝点击