一、使用FileStreams复制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private static void copyFileUsingFileStreams(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) != -1) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}

二、使用FileChannel复制

1
2
3
4
5
6
7
8
9
10
11
12
private static void copyFileUsingFileChannels(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}

三、使用Commons IO复制

1
2
3
4
private static void copyFileUsingApacheCommonsIO(File source, File dest)
throws IOException {
FileUtils.copyFile(source, dest);
}

源码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
private static void doCopyFile(final File srcFile, final File destFile, final boolean preserveFileDate)
throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}

try (FileInputStream fis = new FileInputStream(srcFile);
FileChannel input = fis.getChannel();
FileOutputStream fos = new FileOutputStream(destFile);
FileChannel output = fos.getChannel()) {
final long size = input.size(); // TODO See IO-386
long pos = 0;
long count = 0;
while (pos < size) {
final long remain = size - pos;
count = remain > FILE_COPY_BUFFER_SIZE ? FILE_COPY_BUFFER_SIZE : remain;
final long bytesCopied = output.transferFrom(input, pos, count);
if (bytesCopied == 0) { // IO-385 - can happen if file is truncated after caching the size
break; // ensure we don't loop forever
}
pos += bytesCopied;
}
}

final long srcLen = srcFile.length(); // TODO See IO-386
final long dstLen = destFile.length(); // TODO See IO-386
if (srcLen != dstLen) {
throw new IOException("Failed to copy full contents from '" +
srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen);
}
if (preserveFileDate) {
destFile.setLastModified(srcFile.lastModified());
}
}

四、使用Java7的Files类复制

1
2
3
4
private static void copyFileUsingJava7Files(File source, File dest)
throws IOException {
Files.copy(source.toPath(), dest.toPath());
}

五、测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package snippet;

import org.apache.commons.io.FileUtils;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;

public class CopyFilesExample {

public static void main(String[] args) throws InterruptedException,
IOException {

File source = new File("F:\\INSTALL_HISTORY\\ideaIU-2019.1.1.exe");
File dest = new File("F:\\TEST\\ideaIU-2019.1.1_1.exe");

// copy file using FileStreams
long start = System.nanoTime();
long end;
copyFileUsingFileStreams(source, dest);
System.out.printf("%-50s = %20d\n", "Time taken by FileStreams Copy", (System.nanoTime() - start));

// copy files using java.nio.FileChannelsource = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt");
dest = new File("F:\\TEST\\ideaIU-2019.1.1_2.exe");
start = System.nanoTime();
copyFileUsingFileChannels(source, dest);
end = System.nanoTime();
System.out.printf("%-50s = %20d\n", "Time taken by FileChannels Copy", (System.nanoTime() - start));

// copy file using Java 7 Files classsource = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt");
dest = new File("F:\\TEST\\ideaIU-2019.1.1_3.exe");
start = System.nanoTime();
copyFileUsingJava7Files(source, dest);
end = System.nanoTime();
System.out.printf("%-50s = %20d\n", "Time taken by Java7 Files Copy", (System.nanoTime() - start));

// copy files using apache commons iosource = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt");
dest = new File("F:\\TEST\\ideaIU-2019.1.1_4.exe");
start = System.nanoTime();
copyFileUsingApacheCommonsIO(source, dest);
end = System.nanoTime();
System.out.printf("%-50s = %20d\n", "Time taken by Apache Commons IO Copy", (System.nanoTime() - start));

}

private static void copyFileUsingFileStreams(File source, File dest)
throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
} finally {
input.close();
output.close();
}
}

private static void copyFileUsingFileChannels(File source, File dest)
throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}

private static void copyFileUsingJava7Files(File source, File dest)
throws IOException {
Files.copy(source.toPath(), dest.toPath());
}

private static void copyFileUsingApacheCommonsIO(File source, File dest)
throws IOException {
FileUtils.copyFile(source, dest);
}

}

结果:

1
2
3
4
Time taken by FileStreams Copy                     =           3307186847
Time taken by FileChannels Copy = 355407223
Time taken by Java7 Files Copy = 434544647
Time taken by Apache Commons IO Copy = 2018366048