Java: 复制文件最快、高效率的方法
Android中的文件复制--视频和图片复制
public class FileOpreateUtils {/*** * @param fromFile 被复制的文件* @param toFile 复制的目录文件* @param rewrite 是否重新创建文件* * <p>文件的复制操作方法*/public static void copyfile(File fromFile, File toFile,Boolean rewrite ){if(!fromFile.exists()){return;}if(!fromFile.isFile()){return;}if(!fromFile.canRead()){return;}if(!toFile.getParentFile().exists()){toFile.getParentFile().mkdirs();}if(toFile.exists() && rewrite){toFile.delete();}try {FileInputStream fosfrom = new FileInputStream(fromFile);FileOutputStream fosto = new FileOutputStream(toFile);byte[] bt = new byte[1024];int c;while((c=fosfrom.read(bt)) > 0){fosto.write(bt,0,c);}//关闭输入、输出流fosfrom.close();fosto.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
不考虑多线程优化,单线程文件复制最快的方法是(文件越大该方法越有优势,一般比常用方法快30+%):
- private static void nioTransferCopy(File source, File target) {
- FileChannel in = null;
- FileChannel out = null;
- FileInputStream inStream = null;
- FileOutputStream outStream = null;
- try {
- inStream = new FileInputStream(source);
- outStream = new FileOutputStream(target);
- in = inStream.getChannel();
- out = outStream.getChannel();
- in.transferTo(0, in.size(), out);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- close(inStream);
- close(in);
- close(outStream);
- close(out);
- }
- }
如果需要监测复制进度,可以用第二快的方法(留意buffer的大小,对速度有很大影响):
- private static void nioBufferCopy(File source, File target) {
- FileChannel in = null;
- FileChannel out = null;
- FileInputStream inStream = null;
- FileOutputStream outStream = null;
- try {
- inStream = new FileInputStream(source);
- outStream = new FileOutputStream(target);
- in = inStream.getChannel();
- out = outStream.getChannel();
- ByteBuffer buffer = ByteBuffer.allocate(4096);
- while (in.read(buffer) != -1) {
- buffer.flip();
- out.write(buffer);
- buffer.clear();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- close(inStream);
- close(in);
- close(outStream);
- close(out);
- }
- }
常用的方法1是:
- private static void customBufferBufferedStreamCopy(File source, File target) {
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new BufferedInputStream(new FileInputStream(source));
- fos = new BufferedOutputStream(new FileOutputStream(target));
- byte[] buf = new byte[4096];
- int i;
- while ((i = fis.read(buf)) != -1) {
- fos.write(buf, 0, i);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(fis);
- close(fos);
- }
- }
常用的方法2是:
- private static void customBufferStreamCopy(File source, File target) {
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new FileInputStream(source);
- fos = new FileOutputStream(target);
- byte[] buf = new byte[4096];
- int i;
- while ((i = fis.read(buf)) != -1) {
- fos.write(buf, 0, i);
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- } finally {
- close(fis);
- close(fos);
- }
- }
延伸:
在Java编程中,复制文件的方法有很多,而且经常要用到。我以前一直是缓冲输入输出流来实现的(绝大多数人都是如此),近来在研究JDK文档时发现,用文件通道(FileChannel)来实现文件复制竟然比用老方法快了近三分之一。下面我就来介绍一下如何用文件通道来实现文件复制,以及在效率上的对比
用文件通道的方式来进行文件复制
/**
* 使用文件通道的方式复制文件
*
* @param s
* 源文件
* @param t
* 复制到的新文件
*/
public void fileChannelCopy(File s, File t) {
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(s);
fo = new FileOutputStream(t);
in = fi.getChannel();//得到对应的文件通道
out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
与普通的缓冲输入输出流的复制效率的对比
普通的缓冲输入输出流代码:
测试代码:
输出结果:
由此可见,FileChannel复制文件的速度比BufferedInputStream/BufferedOutputStream复制文件的速度快了近三分之一。在复制大文件的时候更加体现出FileChannel的速度优势。而且FileChannel是多并发线程安全的。
Java: 复制文件最快、高效率的方法相关推荐
- JAVA复制文件夹的第二种方法
包含复制所有目录和文件 package b;import java.io.*;public class Test01 {public static void main(String[] args) t ...
- java 复制文件_Java中复制文件的4种方法
Java拷贝文件是一种非常常见的操作.但是java.io.File类没有任何快捷方法可以将文件从源复制到目标文件.在这里,我们将了解学习可以在java中复制文件的四种不同方法. 方法一:使用Strea ...
- java复制文件_java多种文件复制方式以及效率比较
1.背景 java复制文件的方式其实有很多种,可以分为 - 传统的字节流读写复制FileInputStream,FileOutputStream,BufferedInputStream,Buffere ...
- java.util.zip 用法,Java压缩文件工具类ZipUtil使用方法代码示例
本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io. ...
- java复制文件的4种方式及拷贝文件到另一个目录下与删除单个文件和删除整个文件夹
文章目录 1.使用FileStreams复制 2.使用FileChannel复制 3.使用Commons IO复制 4.使用Java7的Files类复制 5.下面看下java拷贝文件到另一个目录下的实 ...
- Java程序片:Java复制文件
第一种方法(输入输出流): public void copyFile(File fromFile,File toFile) throws Exception{FileInputStream in=ne ...
- windows server2008无法将本地文件复制到远程计算机,windows2008/2012无法从本地复制文件到远程服务器处理方法...
问题现象:远程登录到服务器后,从本地电脑复制文件无法在服务器里面粘贴(在服务器里面粘贴后是之前在服务器内部复制的信息)确认在远程连接工具中勾选了剪贴板.如图 问题原因:可能是 rdpclip.exe ...
- 安装程序无法复制文件 Atapi.sys的解决方法
试图安装 Windows XP Service Pack 2.Windows XP Tablet PC Edition 2005 或 Windows Server 2003 Service Pack ...
- Java 复制文件夹及文件
这里用到了两种复制文件夹及文件的方法 方法1:逐层复制文件夹&文件 参考博客1 @Controller public class UploadController {@Value(" ...
最新文章
- 软工实践原型设计——PaperRepositories
- 获取数组第N个元素的方法
- c语言程序做四则运算还要余数,大整数四则运算 高质量C语言程序.doc
- QIIME 2教程. 18序列双端合并read-joining(2020.11)
- 在日志文件中输出当前时间
- python程序设计教材浅显易懂_这些python自学技巧,你不会?
- javascript --- 文件上传即时预览 闭包实现多图片即时预览
- Linux下Zend Framework的“Invalid Controller Specified”问题
- windows server上存储提示“由于管理员设置的策略,该磁盘处于脱机状态”
- 如何让Mac在 Finder 顶部显示完整的文件路径
- 解决IIS 下ASP程序错误只显示500,不显示具体错误描述的问题!
- java计算机毕业设计高考填报信息系统源码+数据库+系统+lw文档+部署
- 华三交换机怎么关闭445端口通信
- 参考文献编号[9]之后出现空格解决方法
- 前端面试题,速看webP,把握住网页提速小细节!亲测可用!
- 曼哈顿算法公式_距离计算方法总结
- pytorch锁死在dataloader(训练时卡死)
- Celery异步任务
- Rocket MQ发送消息报错: service not available now
- sm缩写代表什么意思_什么是量比?个股量比代表什么意思
热门文章
- “万维网之父”发文阐述其下一个网络时代:将数据与应用分离,互联网去中心化正在路上...
- 哥伦比亚大学AI实验室主任Hod Lipson:阻碍无人驾驶技术发展的7个误区
- 量子计算陷入难解困境,未来发展何去何从?
- Uber致人死亡或为自动驾驶肇事责任 没有判例可循
- 总监调岗至前台,企业被判赔偿26万,法院:“侮辱性调岗”违法!
- Chrome 94 加入网页开发新技术,或有助于提高云游戏体验
- 内部黑客讲述:Twitter 史上最大规模攻击事件始末!
- 用Java实现Stream流处理中的滑窗
- 百度首席科学家吴恩达谈百度人工智能项目进展情况
- Flask入门 表单Flask-wtf form原生与Bootstrap渲染(七)