NIO分散読み取り(散布読み込み)集約と書き込み(書き込みを収集)

分散:(キャッシュ複数の1つのチャンネルからデータを読み取ります)

         データがバッファから読み出されたチャネルは、(データが順次チャネルバッファを満たすから読み出しバッファするために)分散しました

チャネルへの書き込みバッファ:(複数の集計データ)

「集計」バッファのデータは複数のチャネルに書き込まれます。

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


public class ChannelTest {

    @Test
    public void channel4() throws IOException {
        RandomAccessFile raf1=new RandomAccessFile("文件路径","文件模式rw");
        //1,获取通道
        FileChannel channel1=raf1.getChannel();

        //2,分配指定大小的缓冲区
        ByteBuffer byteBuffer1=ByteBuffer.allocate(100);
        ByteBuffer byteBuffer2=ByteBuffer.allocate(1024);

        //3,分散读取
        ByteBuffer[]buffers={byteBuffer1,byteBuffer2};
        channel1.read(buffers);
        for (ByteBuffer bf:buffers){
            bf.flip();
        }
        //4,聚集写入
        RandomAccessFile raf2=new RandomAccessFile("目标文件路径","模式rw");
        FileChannel channel2 = raf2.getChannel();
        channel2.write(buffers);

        //5,关闭通道
        channel1.close();
        channel2.close();

    }
}

 

公開された242元の記事 ウォン称賛13 ビュー10000 +

おすすめ

転載: blog.csdn.net/qq_41813208/article/details/103839353
おすすめ