尽管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. sqlserver 变量

    变量:分为全局变量和局部变量全部变量:以@@声明,为系统变量,所有实例都能访问,用户只能访问,不能赋值局部变量:生命周期只在一个批处理内有效, 局部变量经常使用的三种用途:1 在循环语句中记录循环的次 ...

  2. Dr.com校园网客户端故障解决方法

    一,登录客户端的时候提示“登录超时失败” 解决办法:单击桌面右下角的联网图标然后右键,打开网络和共享中心,在左侧导航栏中找到更改网络适配器,找到本地连接(或者无线连接),点击右键找到详细信息,看看ip ...

  3. 【JAVA】浅谈java枚举类

    一.什么情况下使用枚举类? 有的时候一个类的对象是有限且固定的,这种情况下我们使用枚举类就比较方便? 二.为什么不用静态常量来替代枚举类呢? public static final int SEASO ...

  4. iOS -数据持久化之CoreData

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

  5. GDI+中发生一般性错误(转载)

    在开发.NET应用中,使用 System.Drawing.Image.Save 方法而导致“GDI+ 中发生一般性错误”的发生,通常有以下三种原因:1. 相应的帐户没有写权限.解决方法:赋予 NETW ...

  6. 6.5 k个已排好序链表合并为一个排序链表

    1 建立链表(带哨兵位的)2 建立最小堆方法3 合并已排好序的k个链表 typedef int DataType; //建立链表 class Link { private: struct Node { ...

  7. 责任链模式(Chain of Responsibility Pattern)

    责任链模式:可以为某个请求创建一个对象链.每个对象依序检查此请求,并对其处理,或者把它传给链中的下一个对象. 责任链上的对象负责处理请求,客户只需要将请求发送到责任链上即可,无需关心处理的细节和请求的 ...

  8. C#生成二维码名片

    摘自<31天学会CRM项目开发<C#编程入门级项目实战>> 本例将使用ThoughtWorks.QRCode类库生成二维码名片.正式编码前,先了解一下什么是vCard?它是一种 ...

  9. lamp 中基本配置常识

    // apache// 禁止访问目录// 开启 url重写// 重写定义错误页面// 日志分页// 增加并发连接数// 设置连接连接的时间 // threadsPerChild // 每个进程的线程数 ...

  10. firemonkey 得到屏幕信息

    type TO_MONITOR = class hm: HMONITOR; end; function EnumMonitorsProc(hm: HMONITOR; dc: HDC; r: PRect ...