Dispersion :( read data from one channel to a plurality of cache)
Channel the data read from the Buffer dispersed (in order to buffer data read from sequentially fill Channel Buffer)
Aggregate data from a plurality of write buffer :( to a channel)
The "aggregate" Buffer data is written to the plurality of channels
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();
}
}