這是在Google搜索到的一片文章,介紹了4種java語言中複製文件的方法。轉載地址:http://www.journaldev.com/861/4-ways-to-copy-file-in-java
java.io.File class doesn’t have any shortcut method to copy file from source to destination. Here we will learn about five different ways we can copy file in java.
1. Using Stream: This is the conventional way of file copy in java, here we create two Files, source and destination. Then we create InputStream from source and write it to destination file using OutputStream.
Here is the method that can be used to copy file using streams.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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(); } } |
2. Using java.nio.channels.FileChannel: Java NIO classes were introduced in Java 1.4 and FileChannel can be used to copy file in java. According to transferFrom() method javadoc, this way of copy file is supposed to be faster than using Streams to copy files.
Here is the method that can be used to copy file using FileChannel.
1
2
3
4
5
6
7
8
9
10
11
12
|
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(); } } |
3. Using Apache Commons IO: Apache Commons IO FileUtils.copyFile(File srcFile, File destFile) can be used to copy file in java. If you are already using Apache Commons IO in your project, it makes sense to use this for code simplicity. Internally it uses Java NIO FileChannel, so you can avoid this wrapper method if you are not already using it for other functions.
Here is the method for using apache commons io for file copy in java.
1
2
3
|
private
static
void
copyFileUsingApacheCommonsIO(File source, File dest) throws
IOException { FileUtils.copyFile(source,
dest); } |
4. Java 7 Files class: If you are working on Java 7, you can use Files class copy() method to copy file in java. It uses File System providers to copy the files.
1
2
3
|
private
static
void
copyFileUsingJava7Files(File source, File dest) throws
IOException { Files.copy(source.toPath(),
dest.toPath()); } |
Now to find out which is the fastest method, I wrote a test class and executed above methods one-by-one for copy file of 1 GB. In each call, I used different files to avoid any benefit to later methods because of caching.
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
|
package
com.journaldev.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/pankaj/tmp/source.avi" ); File
dest = new
File( "/Users/pankaj/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/pankaj/tmp/sourceChannel.avi" ); dest
= new
File( "/Users/pankaj/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/pankaj/tmp/sourceApache.avi" ); dest
= new
File( "/Users/pankaj/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/pankaj/tmp/sourceJava7.avi" ); dest
= new
File( "/Users/pankaj/tmp/destJava7.avi" ); start
= System.nanoTime(); copyFileUsingJava7Files(source,
dest); System.out.println( "Time
taken by Java7 Files Copy = " +(System.nanoTime()-start));
} } |
Here is the output of above program, note that I commented above code to make sure every time only one method is used for file copy.
1
2
3
4
|
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 |
From the output it’s clear that Stream Copy is the best way to copy File in Java.
Ideally you should play around different ways to copy files based on your average size of file.
總結:Apache Commons IO這樣方法內部使用的是FileChannelImpl,與Channel Copy可以說同屬於一種方法,速度基本差不多。JDK7 文件複製 最終使用的也是流處理,只不過過程比較複雜。
Day's Over.....