栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

目录
    • 一、FileChannel方法说明
      • 1.1、FileChannel工作模式
      • 1.2、FileChannel获取
      • 1.3、FileChannel读取
      • 1.4、FileChannel写入
      • 1.5、FileChannel关闭
      • 1.6、FileChannel位置
      • 1.7、FileChannel大小
    • 二、两个 Channel 传输数据示例(一)
    • 三、两个 Channel 传输数据示例,传输数据 >2G(二)

一、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.4、FileChannel写入
  • 在 while 中调用 channel.write 是因为 write 方法并不能保证一次将 buffer 中的内容全部写入 channel

    ByteBuffer buffer = ...;
    buffer.put(...); // 存入数据
    buffer.flip();   // 切换读模式
    
    while(buffer.hasRemaining()) {
        channel.write(buffer);
    }
    
1.5、FileChannel关闭
  • channel必须关闭,不过调用了 FileInputStream、FileOutputStream 或者 RandomAccessFile 的 close 方法会间接地调用 channel 的 close 方法。
1.6、FileChannel位置
  • 获取当前位置

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

    long newPos = ...;
    channel.position(newPos);
    
  • 设置当前位置时,如果设置为文件的末尾
    (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;
    
    
    public class TestFileChannelTransferTo {
        public static void main(String[] args) {
            transferTo1();
        }
    
        //通过FileChannel传输数据(方式一)
        public static void transferTo1(){
            
            try (FileChannel from = new FileInputStream("file/from.txt").getChannel();
                 FileChannel to = new FileOutputStream("file/to.txt").getChannel();
                 )
            {
                
    
                from.transferTo(0,from.size(),to);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
  • 运行代码后查看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;
    
    
    public class TestFileChannelTransferTo {
        public static void main(String[] args) {
            transferTo2();
        }
    
    
        //通过FileChannel传输数据(方式二),传输数据 >2G
        public static void transferTo2(){
            
            try (FileChannel from = new FileInputStream("file/from.txt").getChannel();
                 FileChannel to = new FileOutputStream("file/to.txt").getChannel();
            )
            {
                
                for (long left =from.size(); left >0;){
                    
                    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();
            }
        }
    }
    
  • 运行代码后查看to.txt文件中的内容,如下图:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027581.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号