Java复制文件–用Java复制文件的4种方法
Java复制文件是非常常见的操作。
但是java.io.File类没有任何快捷方式可以将文件从源复制到目标。
其中我们将学习在Java中复制文件的四种不同方式。
Java复制文件
- Java复制文件–流
这是Java中文件复制的常规方式。
其中我们创建两个文件-源文件和目标文件。
然后,我们从源创建InputStream并使用OutputStream进行java复制文件操作,将其写入目标文件。
这是可用于使用流的Java复制文件的方法。
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
- Java复制文件– java.nio.channels.FileChannel
Java NIO类是在Java 1.4中引入的,FileChannel可用于在Java中复制文件。
根据transferFrom()方法javadoc,这种复制文件的方式应该比对Java复制文件使用Streams更快。
这是可用于使用FileChannel复制文件的方法。
private static void copyFileUsingChannel(File source, File dest) throws IOException {
FileChannel sourceChannel = null;
FileChannel destChannel = null;
try {
sourceChannel = new FileInputStream(source).getChannel();
destChannel = new FileOutputStream(dest).getChannel();
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
}finally{
sourceChannel.close();
destChannel.close();
}
}
- Java复制文件– Apache Commons IO FileUtils
Apache Commons IO FileUtils.copyFile(File srcFile,File destFile)可用于在Java中复制文件。
如果您已经在项目中使用Apache Commons IO,则使用它来简化代码是很有意义的。
它在内部使用Java NIO FileChannel,因此如果尚未将其用于其他功能,则可以避免使用该包装器方法。
这是将apache commons io用于Java复制文件操作的方法。
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
- Java复制文件–文件类
如果您使用的是Java 7或者更高版本,则可以使用Files类的copy()方法在Java中复制文件。
它使用文件系统提供程序来复制文件。
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
现在,找出哪种方法最快,我编写了一个测试类,并针对1 GB的复制文件逐个执行了上述方法。
在每个调用中,我都使用了不同的文件,以避免由于缓存而对以后的方法产生任何好处。
package com.theitroad.files;
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;
public class JavaCopyFile {
public static void main(String[] args) throws InterruptedException, IOException {
File source = new File("/Users/hyman/tmp/source.avi");
File dest = new File("/Users/hyman/tmp/dest.avi");
//copy file conventional way using Stream
long start = System.nanoTime();
copyFileUsingStream(source, dest);
System.out.println("Time taken by Stream Copy = "+(System.nanoTime()-start));
//copy files using java.nio FileChannel
source = new File("/Users/hyman/tmp/sourceChannel.avi");
dest = new File("/Users/hyman/tmp/destChannel.avi");
start = System.nanoTime();
copyFileUsingChannel(source, dest);
System.out.println("Time taken by Channel Copy = "+(System.nanoTime()-start));
//copy files using apache commons io
source = new File("/Users/hyman/tmp/sourceApache.avi");
dest = new File("/Users/hyman/tmp/destApache.avi");
start = System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
System.out.println("Time taken by Apache Commons IO Copy = "+(System.nanoTime()-start));
//using Java 7 Files class
source = new File("/Users/hyman/tmp/sourceJava7.avi");
dest = new File("/Users/hyman/tmp/destJava7.avi");
start = System.nanoTime();
copyFileUsingJava7Files(source, dest);
System.out.println("Time taken by Java7 Files Copy = "+(System.nanoTime()-start));
}
}
这是上述程序的输出,请注意,我在上面的代码中添加了注释,以确保每次仅将一种方法用于Java文件复制操作。
Time taken by Stream Copy = 44582575000 Time taken by Channel Copy = 104138195000 Time taken by Apache Commons IO Copy = 108396714000 Time taken by Java7 Files Copy = 89061578000
从输出中可以清楚地看出,Stream Copy是用Java复制File的最佳方法。
但这是一个非常基本的测试。
如果您正在处理一个性能密集型项目,则应尝试使用Java复制文件的不同方法,并记下时间安排,以找到适合您项目的最佳方法。
您还应该根据文件的平均大小来尝试不同的Java复制文件方式。

