Java 复制文件是一个非常常见的操作,但是 java.io.File 类没有任何快捷的方法来复制文件从源到目的地. 在这里,我们将了解四种不同的方式,我们可以 复制文件在 java。
Java 复制文件
- Java 复制文件 - 流
这里我们创建两个文件 - 源和目的地. 然后我们创建 InputStream从源,并使用 OutputStream将其写入目的地文件用于 Java 复制文件操作。
1private static void copyFileUsingStream(File source, File dest) throws IOException {
2 InputStream is = null;
3 OutputStream os = null;
4 try {
5 is = new FileInputStream(source);
6 os = new FileOutputStream(dest);
7 byte[] buffer = new byte[1024];
8 int length;
9 while ((length = is.read(buffer)) > 0) {
10 os.write(buffer, 0, length);
11 }
12 } finally {
13 is.close();
14 os.close();
15 }
16}
2、Java 复制文件 - java.nio.channels.FileChannel**
Java NIO 类被引入 Java 1.4 和 FileChannel 可以用来复制文件在 java. 根据 transferFrom() 方法 javadoc,这种方式的复制文件应该比使用 Java 复制文件的流更快. 这里是可以用来复制文件使用 FileChannel 的方法。
1private static void copyFileUsingChannel(File source, File dest) throws IOException {
2 FileChannel sourceChannel = null;
3 FileChannel destChannel = null;
4 try {
5 sourceChannel = new FileInputStream(source).getChannel();
6 destChannel = new FileOutputStream(dest).getChannel();
7 destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
8 }finally{
9 sourceChannel.close();
10 destChannel.close();
11 }
12}
- Java 复制文件 - Apache Commons IO FileUtils
Apache Commons IO FileUtils._copyFile(File srcFile, File destFile)_可以用来复制Java中的文件. 如果您已经在您的项目中使用了Apache Commons IO,那么为了代码的简单性,使用此方法是有意义的。内部使用Java NIO FileChannel,因此您可以避免这种包装方法,如果您还没有使用它用于其他功能。
1private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
2 FileUtils.copyFile(source, dest);
3}
Java 复制文件 - 文件类别
如果您正在使用 Java 7 或更高版本,您可以使用 **Files**类 copy() 方法将文件复制到 java。
1private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
2 Files.copy(source.toPath(), dest.toPath());
3}
现在要找出哪种方法是最快的,我写了一个测试类,并执行上面的方法一对一,以复制1GB的文件. 在每个调用中,我使用不同的文件,以避免由于缓存而对后续方法有任何好处。
1package com.journaldev.files;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.nio.channels.FileChannel;
10import java.nio.file.Files;
11
12import org.apache.commons.io.FileUtils;
13
14public class JavaCopyFile {
15
16 public static void main(String[] args) throws InterruptedException, IOException {
17 File source = new File("/Users/pankaj/tmp/source.avi");
18 File dest = new File("/Users/pankaj/tmp/dest.avi");
19
20 //copy file conventional way using Stream
21 long start = System.nanoTime();
22 copyFileUsingStream(source, dest);
23 System.out.println("Time taken by Stream Copy = "+(System.nanoTime()-start));
24
25 //copy files using java.nio FileChannel
26 source = new File("/Users/pankaj/tmp/sourceChannel.avi");
27 dest = new File("/Users/pankaj/tmp/destChannel.avi");
28 start = System.nanoTime();
29 copyFileUsingChannel(source, dest);
30 System.out.println("Time taken by Channel Copy = "+(System.nanoTime()-start));
31
32 //copy files using apache commons io
33 source = new File("/Users/pankaj/tmp/sourceApache.avi");
34 dest = new File("/Users/pankaj/tmp/destApache.avi");
35 start = System.nanoTime();
36 copyFileUsingApacheCommonsIO(source, dest);
37 System.out.println("Time taken by Apache Commons IO Copy = "+(System.nanoTime()-start));
38
39 //using Java 7 Files class
40 source = new File("/Users/pankaj/tmp/sourceJava7.avi");
41 dest = new File("/Users/pankaj/tmp/destJava7.avi");
42 start = System.nanoTime();
43 copyFileUsingJava7Files(source, dest);
44 System.out.println("Time taken by Java7 Files Copy = "+(System.nanoTime()-start));
45 }
46}
这里是上面的程序的输出,请注意我在上面评论了代码,以确保每次只使用一种方法来复制Java文件。
1Time taken by Stream Copy = 44582575000
2Time taken by Channel Copy = 104138195000
3Time taken by Apache Commons IO Copy = 108396714000
4Time taken by Java7 Files Copy = 89061578000
从输出,很明显,流复制是复制Java文件的最佳方法,但这是一个非常基本的测试. 如果你正在从事一个高性能的项目,那么你应该尝试不同的方法来复制Java文件,并记下时间来找出你的项目的最佳方法。你也应该玩不同的方式来复制Java文件,基于你的平均文件大小。我已经创建了一个YouTube视频的4种方式来复制Java文件,你可以观看它来了解更多。