NIO--FileChannel基本使用:读文件、写文件、文件传输

该篇简单介绍下文件channel的基本使用:读文件 channel.read()、写文件 channel.write()、文件传输 channel.transferFrom() + channel.transferTo() 。


1、读文件

通过FileChanel+ByteBuffer读取文件内容

public static void main(String[] args) throws IOException {
        String filePath="C:\\Users\\Administrator\\Desktop\\test.txt";
        // 1、创建一个FileChannel;  rw表示读写模式
        RandomAccessFile randomAccessFile=new RandomAccessFile(filePath,"rw");
        FileChannel channel = randomAccessFile.getChannel();
        // 2、创建buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 2、通过channel读文件到buffer中
        int byteNum = channel.read(byteBuffer);
        // -1 表示读取到最后一行了
        while (byteNum!=-1){
            System.out.println("读取了:"+byteNum);
            // buffer中是否有内容
            while (byteBuffer.hasRemaining()){
                // 读写切换(内部游标操作)
                byteBuffer.flip();
                byte b = byteBuffer.get();
                System.out.println((char)b);
            }
            // 清空缓冲区
            byteBuffer.clear();
            // 清空缓冲区后继续读文件
            byteNum=channel.read(byteBuffer);
        }
        randomAccessFile.close();
    }

2、写文件

 public static void main(String[] args) throws IOException {
        // 1、创建channel;  文件不存在会自动创建
        String filePath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
        RandomAccessFile randomAccessFile=new RandomAccessFile(filePath, "rw");
        // 2、创建channel
        FileChannel fileChannel = randomAccessFile.getChannel();
        // 3、创建一个buffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        // 4、向buffer中写一个内容
        String tempContext="this is context :123456789";
        byteBuffer.put(tempContext.getBytes());
        // 5、读写切换
        byteBuffer.flip();
        while (byteBuffer.hasRemaining()){ // 如果有剩余内容
            fileChannel.write(byteBuffer);
            // 因为不知道是否buffer中还有内容,需要循环调用
        }
        // 6、关闭channel
        fileChannel.close();
        System.out.println("结束");
    }

3、文件传输

3.1 transferTo()

吧当前channel内容传输到另外一个channel,用来把一个文件内容复制到另外一个文件中。

public static void main(String[] args) throws IOException {
        // 源文件的channel
        String fromPath="C:\\Users\\Administrator\\Desktop\\test.txt";
        RandomAccessFile randomAccessFile1=new RandomAccessFile(fromPath,"rw");
        FileChannel fromChannel = randomAccessFile1.getChannel();
        // 要复制到的目标文件的channel
        String targetPath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
        RandomAccessFile randomAccessFile=new RandomAccessFile(targetPath, "rw");
        FileChannel targetChannel = randomAccessFile.getChannel();

        // 三个参数:  从哪开始复制、复制多少、复制到哪个channel中
        fromChannel.transferTo(0, fromChannel.size(), targetChannel);

        fromChannel.close();
        targetChannel.close();
        System.out.println("完毕");
    }

3.2 transferFrom()

把一个channel中的内容传输到当前channel。

public static void main(String[] args) throws IOException {
        // 源文件的channel
        String fromPath="C:\\Users\\Administrator\\Desktop\\test.txt";
        RandomAccessFile randomAccessFile1=new RandomAccessFile(fromPath,"rw");
        FileChannel fromChannel = randomAccessFile1.getChannel();
        // 要复制到的目标文件的channel
        String targetPath="C:\\Users\\Administrator\\Desktop\\test_write.txt";
        RandomAccessFile randomAccessFile=new RandomAccessFile(targetPath, "rw");
        FileChannel targetChannel = randomAccessFile.getChannel();

        // 三个参数: 从哪个channel来的、 从哪开始复制、复制多少
        targetChannel.transferFrom(fromChannel, 0, fromChannel.size());

        fromChannel.close();
        targetChannel.close();
        System.out.println("完毕");
    }
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值