Java使用FileChannel进行文件拷贝(提升拷贝效率)

Java使用FileChannel进行文件拷贝

FileChannel属于nio,FileChannel底层会利用操作系统的零拷贝进行优化,效率较io高。

导包

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

1.当所拷贝的文件小于2G时

代码

    public void channelCopy(String sourcePath,String destPath){

        try {
            FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

说明

sourcePath:源地址,如E:\xx\yy\a.txt
destPath:目的地址,如D:\yy\a.txt

sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
参数说明
0:表示从源文件的什么位置开始拷贝。
sourceChannel.size():拷贝多大。
destChannel:拷贝到那里去。
该方法的返回值为真实拷贝的size。该方法最大拷贝2G,超出2G的部分将丢弃。

解决掉异常,以及流关闭的完整封装

public void channelCopy(String sourcePath,String destPath){
        FileChannel sourceChannel=null;
        FileChannel destChannel=null;

        try {
            sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            sourceChannel.transferTo(0,sourceChannel.size(),destChannel);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (sourceChannel!=null){
                    sourceChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (destChannel!=null){
                    destChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

2.所拷贝内容大于2G

 //超过 2g 大小的文件传输
    public void channelCopy(String sourcePath,String destPath){

        try {
            FileChannel sourceChannel = new FileInputStream(sourcePath).getChannel();
            FileChannel destChannel = new FileOutputStream(destPath).getChannel();
            //所要拷贝的原文件大小
            long size=sourceChannel.size();
            for (long left=size;left>0;){
                //transferSize所拷贝过去的真实长度
                //size - left计算出下次要拷贝的位置
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                //还剩余多少
                left=left-transferSize;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

解决掉异常,以及流关闭的完整封装

//超过 2g 大小的文件传输
    public void channelCopy(String sourcePath,String destPath){

        FileChannel sourceChannel=null;
        FileChannel destChannel=null;
        try {
            sourceChannel = new FileInputStream(sourcePath).getChannel();
            destChannel = new FileOutputStream(destPath).getChannel();

            long size=sourceChannel.size();
            for (long left=size;left>0;){
                long transferSize = sourceChannel.transferTo((size - left), left, destChannel);
                left=left-transferSize;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (sourceChannel!=null){
                    sourceChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (destChannel!=null){
                    destChannel.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

齊 天 大 聖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值