• Netty——FileChannel中方法说明与代码示例


    一、FileChannel方法说明

    1.1、FileChannel工作模式

    • FileChannel 只能工作在阻塞模式下。

    1.2、FileChannel获取

    • 不能直接打开 FileChannel,必须通过 FileInputStream、FileOutputStream 或者 RandomAccessFile 来获取 FileChannel,它们都有 getChannel 方法。
    • 通过 FileInputStream 获取的 channel 只能读。
    • 通过 FileOutputStream 获取的 channel 只能写。
    • 通过 RandomAccessFile 是否能读写根据构造 RandomAccessFile 时的读写模式决定。

    1.3、FileChannel读取

    • 从 channel 读取数据填充 ByteBuffer,返回值表示读到了多少字节,-1 表示到达了文件的末尾。

      int readBytes = channel.read(buffer);
      
      • 1

    1.4、FileChannel写入

    • 在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel

      ByteBuffer buffer = ...;
      buffer.put(...); // 存入数据
      buffer.flip();   // 切换读模式
      
      while(buffer.hasRemaining()) {
          channel.write(buffer);
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    1.5、FileChannel关闭

    • channel必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法。

    1.6、FileChannel位置

    • 获取当前位置

      long pos = channel.position();
      
      • 1
    • 设置当前位置

      long newPos = ...;
      channel.position(newPos);
      
      • 1
      • 2
    • 设置当前位置时,如果设置为文件的末尾
      (1)、这时读取会返回 -1
      (2)、这时写入,会追加内容,但要注意如果 position 超过了文件末尾,再写入时在新内容和原末尾之间会有空洞(00)

    1.7、FileChannel大小

    • 使用 size 方法获取文件的大小

    二、两个 Channel 传输数据示例(一)

    • 创建一个from.txt文件,内容如下:在这里插入图片描述

    • 创建一个文件内容为空的to.txt文件,如下:
      在这里插入图片描述

    • 示例代码

      package com.example.nettytest.nio.day2;
      
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.nio.channels.FileChannel;
      
      /**
       * @description: 通过两个Channel传输数据示例
       * @author: xz
       * @create: 2022-07-26 21:45
       */
      public class TestFileChannelTransferTo {
          public static void main(String[] args) {
              transferTo1();
          }
      
          //通过FileChannel传输数据(方式一)
          public static void transferTo1(){
              /**
               * 新建2个FileChannel
               * from 表示从from.txt读取内容
               * to 表示向to.txt写入内容
               * */
              try (FileChannel from = new FileInputStream("file/from.txt").getChannel();
                   FileChannel to = new FileOutputStream("file/to.txt").getChannel();
                   )
              {
                  /**
                   *第一个参数表示起始位置下标
                   *第二个参数表示读取文件内容的大小
                   *第三个参数表示向哪个文件里写
                   * transferTo 数据传输方法
                   * */
      
                  from.transferTo(0,from.size(),to);
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
    • 运行代码后查看to.txt文件中的内容,如下图:
      在这里插入图片描述

    三、两个 Channel 传输数据示例,传输数据 >2G(二)

    • 创建一个from.txt文件,内容如下:在这里插入图片描述

    • 创建一个文件内容为空的to.txt文件,如下:
      在这里插入图片描述

    • 示例代码

      package com.example.nettytest.nio.day2;
      
      import java.io.FileInputStream;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.nio.channels.FileChannel;
      
      /**
       * @description: 通过两个Channel传输数据示例
       * @author: xz
       * @create: 2022-07-26 21:45
       */
      public class TestFileChannelTransferTo {
          public static void main(String[] args) {
              transferTo2();
          }
      
      
          //通过FileChannel传输数据(方式二),传输数据 >2G
          public static void transferTo2(){
              /**
               * 新建2个FileChannel
               * from 表示从from.txt读取内容
               * to 表示向to.txt写入内容
               * */
              try (FileChannel from = new FileInputStream("file/from.txt").getChannel();
                   FileChannel to = new FileOutputStream("file/to.txt").getChannel();
              )
              {
                  /**
                   * 此方式效率高,底层会利用操作系统的零拷贝进行优化 传输数据 >2G
                   * left 表示 变量代表还剩余多少字节
                   * left =from.size() 表示初始大小为from.txt文件内容的大小
                   * left >0 表示初始时大小 > 0
                   * */
                  for (long left =from.size(); left >0;){
                      /**from.size() - left 表示起始位置下标
                       * left 表示剩余多少字节下标
                       */
                      System.out.println("position:" + (from.size() - left) + " left:" + left);
                      //transferTo 数据传输方法
                      left -= from.transferTo((from.size() - left), left, to);
                  }
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
    • 运行代码后查看to.txt文件中的内容,如下图:
      在这里插入图片描述

  • 相关阅读:
    复杂网络建模(一)
    哪些浏览器受到用户欢迎?分享这两款安全浏览器
    零代码编程:用ChatGPT批量自动下载archive.org上的音频书
    数据结构之链表(算法之初)
    Python 中的方法重载
    数论——中国剩余定理及其扩展详解
    物联网时代的等保测评:保障万物互联的安全
    centos docker服务问题
    实战PyQt5:149-QChart图表之图例设置
    Python 零基础入门数据分析实战之小费数据集应用
  • 原文地址:https://blog.csdn.net/li1325169021/article/details/126003666