Java拷贝文件是一种非常常见的操作。但是java.io.File
类没有任何快捷方法可以将文件从源复制到目标文件。在这里,我们将了解学习可以在java中复制文件的四种不同方法。
方法一:使用Stream
这是java中传统的文件复制方式。这里创建两个文件 - source
和destination
。然后从source
创建InputStream
并使用OutputStream
将它写入destination
文件以进行复制文件操作。
下面是使用流来复制文件的示例代码。
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.nio.channels.FileChannel类
Java NIO
类在Java 1.4中引入,FileChannel
类可用于复制文件。它的transferFrom()
方法就可以完成,这种复制文件的方式应该比Stream
复制文件更快。
以下是使用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();
}
}
方法三: 使用FileUtils
可通过使用Apache Commons IO中的FileUtils.copyFile(File srcFile,File destFile)
可用于复制文件。如果已经在项目中使用Apache Commons IO,那么使用它来简化代码是比较容易的。在内部它使用Java NIO FileChannel
,因此如果尚未将它用于其他功能,则可以避免使用此包方法。
以下是使用apache commons io进行java复制文件操作的示例代码。
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
方法四:使用Files类
如果使用Java 7或更高版本,则可以使用Files
类的copy()
方法复制文件。它使用文件系统提供程序来复制文件。
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
知道上面哪个复制方法是最快的吗?下面编写了一个测试类并逐个执行上面的方法,用于1GB的复制文件。在每次调用中,使用不同的文件来避免因为缓存而对以后的方法带来偏差。
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 JavaFile {
public static void main(String[] args) throws InterruptedException, IOException {
File source = new File("D:/users/maxsu/tmp/source.avi");
File dest = new File("D:/users/maxsu/tmp/dest.avi");
// 使用常规方式(Stream)复制文件
long start = System.nanoTime();
copyFileUsingStream(source, dest);
System.out.println("Time taken by Stream = "+(System.nanoTime()-start));
//使用java.nio FileChannel复制文件
source = new File("D:/users/maxsu/tmp/sourceChannel.avi");
dest = new File("D:/users/maxsu/tmp/destChannel.avi");
start = System.nanoTime();
copyFileUsingChannel(source, dest);
System.out.println("Time taken by Channel = "+(System.nanoTime()-start));
// 使用 apache commons io复制文件
source = new File("D:/users/maxsu/tmp/sourceApache.avi");
dest = new File("D:/users/maxsu/tmp/destApache.avi");
start = System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
System.out.println("Time taken by Apache Commons IO = "+(System.nanoTime()-start));
// 使用 Java 7 Files类复制文件
source = new File("D:/users/maxsu/tmp/sourceJava7.avi");
dest = new File("D:/users/maxsu/tmp/destJava7.avi");
start = System.nanoTime();
copyFileUsingJava7Files(source, dest);
System.out.println("Time taken by Java7 Files = "+(System.nanoTime()-start));
}
}
下面是上述程序的输出,请注意在上面的代码中注释,以确保每次只有一个方法用于java文件复制操作。
Time taken by Stream = 44582575000
Time taken by Channel = 104138195000
Time taken by Apache Commons IO = 108396714000
Time taken by Java7 Files = 89061578000
从输出中可以清楚地看出,Stream复制是在Java中复制文件的最佳方式。但这是一个非常基本的测试。如果开发一个性能密集型项目,那么应该尝试不同的复制文件方法,并记下时间,以找出项目的最佳方法。