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

x33g5p2x  于2022-08-17 转载在 其他  
字(3.0k)|赞(0)|评价(0)|浏览(505)

一、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 表示到达了文件的末尾。
  1. int readBytes = channel.read(buffer);

1.4、FileChannel写入

  • 在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel
  1. ByteBuffer buffer = ...;
  2. buffer.put(...); // 存入数据
  3. buffer.flip(); // 切换读模式
  4. while(buffer.hasRemaining()) {
  5. channel.write(buffer);
  6. }

1.5、FileChannel关闭

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

1.6、FileChannel位置

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

1.7、FileChannel大小

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

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

  • 创建一个from.txt文件,内容如下:

  • 创建一个文件内容为空的to.txt文件,如下:

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

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

  • 创建一个from.txt文件,内容如下:

  • 创建一个文件内容为空的to.txt文件,如下:

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

相关文章