尽管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种方式的更多相关文章

  1. [JAVA]java复制文件的4种方式

    尽管Java提供了一个可以处理文件的IO操作类. 但是没有一个复制文件的方法. 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候. 然而有几种方法可以进行Java文件复制操作,下面列举出 ...

  2. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  3. Java读写文件的几种方式

    自工作以后好久没有整理Java的基础知识了.趁有时间,整理一下Java文件操作的几种方式.无论哪种编程语言,文件读写操作时避免不了的一件事情,Java也不例外.Java读写文件一般是通过字节.字符和行 ...

  4. java追加文件的几种方式

    import java.io.BufferedWriter; import java.io.FileOutputStream; import java.io.FileWriter; import ja ...

  5. IO之复制文件的四种方式

    1. 使用FileStreams复制 这是最经典的方式将一个文件的内容复制到另一个文件中. 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B. 这是 ...

  6. Java读取文件的几种方式

    package com.mesopotamia.test; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...

  7. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  8. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  9. java遍历文件夹两种方式

    1:非递归方式(有点类似二叉树的非递归遍历,采用链表来存储遍历到的文件夹,如果是文件就直接输出) public void traverseFile(String path){ File[] files ...

随机推荐

  1. Linux Shell脚本编程--cut命令

    cut cut命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields <==用于有特定分隔字符 [root@www ...

  2. Cordova+angularjs+ionic+vs2015开发(五)

    欢迎加群学习:457351423 这里有4000多部学习视频,涵盖各种技术,有需要的欢迎进群学习! 一.创建侧边菜单和导航项目 1.使用VS创建一个Ionic空项目,同时创建一个Ionic SideM ...

  3. 解决IIS7中出现An error occurred on the server when processing the URL错误提示的方法

    相信用过IIS7的朋友在调试程序的时候都遇到过下面这样的错误提示:    An error occurred on the server when processing the URL. Please ...

  4. 浅谈JAVA集合框架(转载)_常用的Vector和HashMap

    原作者滴着:http://www.cnblogs.com/eflylab/archive/2007/01/20/625237.html Java提供了数种持有对象的方式,包括语言内置的Array,还有 ...

  5. vs2012找不到system web optimization命名空间

    今天新装了vs2012,安装完成后,创建了一个mvc4应用程序,创建生成出现了几个错误.通过错误我们的解决方案就是去找引用不到的路径,如何在vs2012中实现呢? 在工具栏中找工具--库程序包管理器- ...

  6. Web前端新人笔记之HeightCharts基础

    通常情况下,Highcharts包含标题(Title).坐标轴(Axis).数据列(Series).数据提示框(Tooltip).图例(Legend).版权信息(Credits)等,高级的还包括导出功 ...

  7. mybatis学习(一)一个在idea下的实例

    今天总结的是mybatis,首先说mybatis是什么? MyBatis 是一个简化和实现了 Java 数据持久化层(persistence layer)的开源框架,它抽象了大量的 JDBC 冗余代 ...

  8. Visual Stuido 2015 Community 使用 GitHub 插件

    微软在Visual Studio 2015产品中,深度整合了GitHub,让VS用户更方便的使用GitHub的服务. 新闻链接: Announcing the GitHub Extension for ...

  9. makefile--Unfound symbol

    Unfound symbol ,库函数的包含问题或者头文件包含问题 makefile对#的识别度太低了,如果使用了,#它之后的可能就不能识别了,然后会报错的Unfound symbol /////// ...

  10. 九度OJ 1547 出入栈 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1547 题目描述: 给定一个初始为空的栈,和n个操作组成的操作序列,每个操作只可能是出栈或者入栈. 要求在操作序列的 ...