资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Java使用FileChannel进行文件拷贝(提升拷贝效率)-创新互联

Java使用FileChannel进行文件拷贝
  • 1.当所拷贝的文件小于2G时
  • 2.所拷贝内容大于2G

目前创新互联建站已为近1000家的企业提供了网站建设、域名、网页空间、网站运营、企业网站设计、成安网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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();
            }
        }
    }

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


文章题目:Java使用FileChannel进行文件拷贝(提升拷贝效率)-创新互联
链接URL:http://cdkjz.cn/article/ddsdce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220