java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类。
但是没有一个复制文件的方法。
复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候。
然而有几种方法可以进行Java文件复制操作,下面列举出4中最受欢迎的方式。
###1. 使用FileStreams复制
这是最经典的方式将一个文件的内容复制到另一个文件中。
使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。
这是第一个方法的代码:
privatestaticvoid copyFileUsingFileStreams(File source,File dest)throwsIOException{InputStream input =null;OutputStream output =null;try{
input =newFileInputStream(source);
output =newFileOutputStream(dest);byte[] buf =newbyte[1024];int bytesRead;while((bytesRead = input.read(buf))>0){
output.write(buf,0, bytesRead);}}finally{
input.close();
output.close();}}
正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。
###2. 使用FileChannel复制
Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。
这是第二种方法的代码:
privatestaticvoid copyFileUsingFileChannels(File source,File dest)throwsIOException{FileChannel inputChannel =null;FileChannel outputChannel =null;try{
inputChannel =newFileInputStream(source).getChannel();
outputChannel =newFileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel,0, inputChannel.size());}finally{
inputChannel.close();
outputChannel.close();}}
###3. 使用Commons IO复制
Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方。它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。基本上,这个类使用Java NIO FileChannel内部。
这是第三种方法的代码:
private static void copyFileUsingApacheCommonsIO(File source,File dest)
throws IOException{FileUtils.copyFile(source, dest);}
###4. 使用Java7的Files类复制
如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。
这是第四个方法的代码:
private static void copyFileUsingJava7Files(File source,File dest)
throws IOException{Files.copy(source.toPath(), dest.toPath());}
###测试
现在看到这些方法中的哪一个是更高效的,我们会复制一个大文件使用每一个在一个简单的程序。
从缓存来避免任何性能明显我们将使用四个不同的源文件和四种不同的目标文件。
让我们看一下代码:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.nio.channels.FileChannel;import java.nio.file.Files;import org.apache.commons.io.FileUtils;publicclassCopyFilesExample{publicstaticvoid main(String[] args)throwsInterruptedException,IOException{File source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt");File dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt");// copy file using FileStreamslong start =System.nanoTime();long end;
copyFileUsingFileStreams(source, dest);System.out.println("Time taken by FileStreams Copy = "+(System.nanoTime()- start));// copy files using java.nio.FileChannel
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt");
start =System.nanoTime();
copyFileUsingFileChannels(source, dest);
end =System.nanoTime();System.out.println("Time taken by FileChannels Copy = "+(end - start));// copy file using Java 7 Files class
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt");
start =System.nanoTime();
copyFileUsingJava7Files(source, dest);
end =System.nanoTime();System.out.println("Time taken by Java7 Files Copy = "+(end - start));// copy files using apache commons io
source =newFile("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt");
dest =newFile("C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt");
start =System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
end =System.nanoTime();System.out.println("Time taken by Apache Commons IO Copy = "+(end - start));}privatestaticvoid copyFileUsingFileStreams(File source,File dest)throwsIOException{InputStream input =null;OutputStream output =null;try{
input =newFileInputStream(source);
output =newFileOutputStream(dest);byte[] buf =newbyte[1024];int bytesRead;while((bytesRead = input.read(buf))>0){
output.write(buf,0, bytesRead);}}finally{
input.close();
output.close();}}privatestaticvoid copyFileUsingFileChannels(File source,File dest)throwsIOException{FileChannel inputChannel =null;FileChannel outputChannel =null;try{
inputChannel =newFileInputStream(source).getChannel();
outputChannel =newFileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel,0, inputChannel.size());}finally{
inputChannel.close();
outputChannel.close();}}privatestaticvoid copyFileUsingJava7Files(File source,File dest)throwsIOException{Files.copy(source.toPath(), dest.toPath());}privatestaticvoid copyFileUsingApacheCommonsIO(File source,File dest)throwsIOException{FileUtils.copyFile(source, dest);}}
####输出:
Time taken by FileStreamsCopy=127572360Time taken by FileChannelsCopy=10449963Time taken by Java7FilesCopy=10808333Time taken by ApacheCommons IO Copy=17971677
正如您可以看到的FileChannels拷贝大文件是最好的方法。如果你处理更大的文件,你会注意到一个更大的速度差。
这是一个示例,该示例演示了Java中四种不同的方法可以复制一个文件。
java复制文件的4种方式的更多相关文章
- [JAVA]java复制文件的4种方式
尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...
- 【文件下载】Java下载文件的几种方式
[文件下载]Java下载文件的几种方式 摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...
- Java读写文件的几种方式
自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...
- java追加文件的几种方式
import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...
- IO之复制文件的四种方式
1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...
- Java读取文件的几种方式
package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- Java下载文件的几种方式
转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...
- java 下载文件的两种方式和java文件的上传
一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...
- java遍历文件夹两种方式
1:非递归方式(有点类似二叉树的非递归遍历,采用链表来存储遍历到的文件夹,如果是文件就直接输出) public void traverseFile(String path){ File[] files ...
随机推荐
- sql的游标使用(转)
游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: ...
- aix 计算性内存和文件内存
经过有客户问AIX topas中内存(memory)一项显示的数值含义: MEMORY Real,MB 4096 % Comp 68.9 % Noncomp 22.6 % Clie ...
- Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法
Ext.Net学习笔记15:Ext.Net GridPanel 汇总(Summary)用法 Summary的用法和Group一样简单,分为两步: 启用Summary功能 在Feature标签内,添加如 ...
- 在centos7中限制kvm虚拟机可访问的资源
最近通过艰苦卓绝的度娘(我很想用谷歌,可是,你懂的),终于搞明白如何在centos7中限制kvm虚拟机可访问的资源了.度娘给出的结果中,大部分都说的很对,然而,却很难照着做,主要原因有两点:1.网上的 ...
- mysql---左连接、右连接、内连接之间的区别与联系
现有两张表 第一张表为男生表,记录了男生的姓名和配偶的编号 第二张表为女生表,记录了女生的姓名和自己的编号 第一种情况:主持人请所有男生都上台,并且带上自己的配偶.这时不管男生有没有配偶都要上台,所以 ...
- Normalize.css:优化重置CSS默认属性
Normalize.css:优化重置CSS默认属性 官方网站:http://necolas.github.io/normalize.css/ 项目仓库:https://github.com/necol ...
- Web前端新人笔记之文本属性
前一段时间因工作时间减缓了更新笔记的时间.我也不知道有没有会观看并且能不能帮到一些初学者,这只是我的一些小随笔而已.当然我也希望的的每一篇随笔都可以帮到更多的想要学习前端开发的初学者们,更希望你们也可 ...
- 怎么用程序获取远程url执行后的图片地址
远程URL:https://121.199.16.229:8890/generate.cgi?rbid=1001&esn=22021434025005&pic=png&coun ...
- nginx 常用的 URL 重写方法
转自:http://www.jbxue.com/article/4727.html Nginx中一些常用的URL 重写方法介绍,有需要的朋友可以参考下.url重写应该不陌生,不管是SEO URL 伪静 ...
- js实现瀑布流的一种简单方法实例分享
现在说瀑布流式布局似乎有点晚了,但是每一项技术都是向着“精”和“简”的方向在不断发展,在发展到极致之前,需要一个相当漫长的过程,因此,从这个角度来说,当瀑布流被应用得越来越多的时候,反而更应该讨论它, ...