FileChannel通道的Scatter/Gather功能

FileChannel通道的Scatter/Gather功能_第1张图片
Scatter:是指从 Channel 中读取的数据“分散”到多个 Buffer 中,按照缓冲区的顺序,从 Channel 中读取的数据依次将 Buffer 填满。
FileChannel通道的Scatter/Gather功能_第2张图片
FileChannel通道的Scatter/Gather功能_第3张图片
Gather:指将多个 Buffer 中的数据聚集到通道。
FileChannel通道的Scatter/Gather功能_第4张图片
先看看分散的方法:
FileChannel通道的Scatter/Gather功能_第5张图片

写一个文件复制的功能:

 File file = new File("D:\\hufan","love.txt");
        File file1 = new File("D:\\hufan","gao.txt");
        file.createNewFile();
        file1.createNewFile();
        FileChannel fileChannel = FileChannel.open(file.toPath());
        //创建3个缓冲区
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(24);
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(100);
        ByteBuffer byteBuffer3 = ByteBuffer.allocate(900);
        //创建缓冲区数组
        ByteBuffer [] byteBuffers = {byteBuffer1,byteBuffer2,byteBuffer3};
        //创建通道
        FileChannel writeChannel = FileChannel.open(file1.toPath(), StandardOpenOption.WRITE);
        while (fileChannel.read(byteBuffers) != -1){
            Arrays.asList(byteBuffers).stream().map(buffer ->{
                //切换模式 读-写
                buffer.flip();
                try {
                    writeChannel.write(buffer);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //清空buffer
                buffer.clear();
                return null;
            }).collect(Collectors.toList());
        }

FileChannel通道的Scatter/Gather功能_第6张图片
FileChannel通道的Scatter/Gather功能_第7张图片
FileChannel通道的Scatter/Gather功能_第8张图片
再看看Gather:

 File file = new File("D:\\hufan","love.txt");
        File file1 = new File("D:\\hufan","lei.txt");
        file.createNewFile();
        file1.createNewFile();
        FileChannel fileChannel = FileChannel.open(file.toPath());
        //创建3个缓冲区
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(24);
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(100);
        ByteBuffer byteBuffer3 = ByteBuffer.allocate(900);
        //创建缓冲区数组
        ByteBuffer [] byteBuffers = {byteBuffer1,byteBuffer2,byteBuffer3};
        //创建通道
        FileChannel writeChannel = FileChannel.open(file1.toPath(), StandardOpenOption.WRITE);
        while (fileChannel.read(byteBuffers) != -1){
            byteBuffer1.flip();
            byteBuffer2.flip();
            byteBuffer3.flip();
            writeChannel.write(byteBuffers);
            byteBuffer1.clear();
            byteBuffer2.clear();
            byteBuffer3.clear();
        }
        fileChannel.close();
        writeChannel.close();
    }

FileChannel通道的Scatter/Gather功能_第9张图片
FileChannel通道的Scatter/Gather功能_第10张图片
直接向文本中写入值:

 File file = new File("D:\\hufan","hufan.txt");
        file.createNewFile();
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        FileChannel channel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(10);
        //直接向缓冲区写入字节
        byteBuffer1.put("i love you".getBytes());
        byteBuffer1.flip();
        channel.write(byteBuffer1);
        channel.close();

FileChannel通道的Scatter/Gather功能_第11张图片
使用带offset和length参数版本的read( ) 和write( )方法
offset值指哪个缓冲区将开始被使用,而不是指数据的offset。这里的length参数指示要使用的缓冲区数量。

 //hufan.txt 文本中存的是 i love you
        File file1 = new File("D:\\hufan","hufan.txt");
        File file2 = new File("D:\\hufan","xiaokeai.txt");
        file1.createNewFile();
        file2.createNewFile();
        //1个字节
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(1);
        //3个字节
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(3);
        FileChannel fileChannel = FileChannel.open(file1.toPath());
        FileChannel fileWrite = FileChannel.open(file2.toPath(), StandardOpenOption.WRITE);
        ByteBuffer[] bytes = {byteBuffer1,byteBuffer2};
            //从第一个缓冲区开始读取,读2个缓冲区
            //两个缓冲区加起来也就4个字节,所以 ilove you 是读不全的,只能读取4个字节
            fileChannel.read(bytes,0,2);
            byteBuffer1.flip();
            fileWrite.write(byteBuffer1);
            byteBuffer1.clear();
            byteBuffer2.flip();
            fileWrite.write(byteBuffer2);
            byteBuffer2.clear();
            fileChannel.close();
            fileWrite.close();
        fileChannel.close();
        fileWrite.close();

FileChannel通道的Scatter/Gather功能_第12张图片

 //hufan.txt 文本中存的是 i love you
        File file1 = new File("D:\\hufan","hufan.txt");
        File file2 = new File("D:\\hufan","xiaokeai.txt");
        file1.createNewFile();
        file2.createNewFile();
        //1个字节
        ByteBuffer byteBuffer1 = ByteBuffer.allocate(1);
        //3个字节
        ByteBuffer byteBuffer2 = ByteBuffer.allocate(3);
        FileChannel fileChannel = FileChannel.open(file1.toPath());
        FileChannel fileWrite = FileChannel.open(file2.toPath(), StandardOpenOption.WRITE);
        ByteBuffer[] bytes = {byteBuffer1,byteBuffer2};
            //从第一个缓冲区开始读取,读1个缓冲区
            //所以 ilove you 是读不全的,只能读取1个字节
            fileChannel.read(bytes,0,1);
            byteBuffer1.flip();
            fileWrite.write(byteBuffer1);
            byteBuffer1.clear();
            fileChannel.close();
            fileWrite.close();
        fileChannel.close();
        fileWrite.close();

FileChannel通道的Scatter/Gather功能_第13张图片
FileChannel通道的Scatter/Gather功能_第14张图片
写博客只是为了方便自己复习知识点!

你可能感兴趣的:(学习)