尽管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. ios fix UIRefreshControl bug

    NS_CLASS_AVAILABLE_IOS(6_0)   UIRefreshControl 有个毛病有时会出bug  动画下拉就不动了,这里给出修复处理: @interface UICollecti ...

  2. 【JAVA】浅谈java枚举类

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

  3. iOS夯实:ARC时代的内存管理

    iOS夯实:ARC时代的内存管理 文章转自 ARC时代的内存管理 什么是ARC Automatic Reference Counting (ARC) is a compiler feature tha ...

  4. C#基础总复习02

    继续更新第二篇: 1:一元运算符:++ -- ++:不管是前加加还是后加加,变量的值最终都会自身加一. 前加加和后加加的区别体现在参与运算的时候,如果是后加加,则首先拿原值参与运算, 运算完成后再自身 ...

  5. mysql学习笔记3

    要用php+mysql 首先要配置环境.现在要先下载wamp(Windows下的Apache+Mysql/MariaDB+Perl/PHP/Python).直接安装就行 可以点下一步的就点下一步,直至 ...

  6. RTTI(Runtime Type Information )

    RTTI 是“Runtime Type Information”的缩写,意思是:运行时类型信息.它提供了运行时确定对象类型的方法.本文将简略介绍 RTTI 的一些背景知识.描述 RTTI 的概念,并通 ...

  7. Entity Framework 学习笔记(1)

    开始从头系统地学习Entity Framework,当前的稳定版本为6.1.3,Nuget主页 http://www.nuget.org/packages/EntityFramework/ 微软喜欢把 ...

  8. 九度OJ 1410 垒积木 -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1410 题目描述: 给你一些长方体的积木,问按以下规则能最多垒几个积木. 1 一个积木上面最多只能垒另一个积木. 2 ...

  9. MyBatis入门教程(基于Mybatis3.2)

    MyBatis和Hibernate一样都是基于ORM的关系型数据库框架 ORM工具的基本思想: 1.从配置文件(通常是XML配置文件中)得到 sessionfactory. 2. 由sessionfa ...

  10. 组织http请求

    post方式 string stratTime=""; string end=""://要拼接的参数 string postURL = "http:/ ...