java 复制文件_Java中复制文件的4种方法
Java拷貝文件是一種非常常見的操作。但是java.io.File類沒有任何快捷方法可以將文件從源復(fù)制到目標(biāo)文件。在這里,我們將了解學(xué)習(xí)可以在java中復(fù)制文件的四種不同方法。
方法一:使用Stream
這是java中傳統(tǒng)的文件復(fù)制方式。這里創(chuàng)建兩個(gè)文件 - source和destination。然后從source創(chuàng)建InputStream并使用OutputStream將它寫入destination文件以進(jìn)行復(fù)制文件操作。
下面是使用流來復(fù)制文件的示例代碼。
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類可用于復(fù)制文件。它的transferFrom()方法就可以完成,這種復(fù)制文件的方式應(yīng)該比Stream復(fù)制文件更快。
以下是使用FileChannel復(fù)制文件的示例代碼。
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)可用于復(fù)制文件。如果已經(jīng)在項(xiàng)目中使用Apache Commons IO,那么使用它來簡化代碼是比較容易的。在內(nèi)部它使用Java NIO FileChannel,因此如果尚未將它用于其他功能,則可以避免使用此包方法。
以下是使用apache commons io進(jìn)行java復(fù)制文件操作的示例代碼。
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
方法四:使用Files類
如果使用Java 7或更高版本,則可以使用Files類的copy()方法復(fù)制文件。它使用文件系統(tǒng)提供程序來復(fù)制文件。
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
知道上面哪個(gè)復(fù)制方法是最快的嗎?下面編寫了一個(gè)測(cè)試類并逐個(gè)執(zhí)行上面的方法,用于1GB的復(fù)制文件。在每次調(diào)用中,使用不同的文件來避免因?yàn)榫彺娑鴮?duì)以后的方法帶來偏差。
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");
// 使用常規(guī)方式(Stream)復(fù)制文件
long start = System.nanoTime();
copyFileUsingStream(source, dest);
System.out.println("Time taken by Stream = "+(System.nanoTime()-start));
//使用java.nio FileChannel復(fù)制文件
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復(fù)制文件
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類復(fù)制文件
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));
}
}
下面是上述程序的輸出,請(qǐng)注意在上面的代碼中注釋,以確保每次只有一個(gè)方法用于java文件復(fù)制操作。
Time taken by Stream = 44582575000
Time taken by Channel = 104138195000
Time taken by Apache Commons IO = 108396714000
Time taken by Java7 Files = 89061578000
從輸出中可以清楚地看出,Stream復(fù)制是在Java中復(fù)制文件的最佳方式。但這是一個(gè)非?;镜臏y(cè)試。如果開發(fā)一個(gè)性能密集型項(xiàng)目,那么應(yīng)該嘗試不同的復(fù)制文件方法,并記下時(shí)間,以找出項(xiàng)目的最佳方法。
¥ 我要打賞
糾錯(cuò)/補(bǔ)充
收藏
加QQ群啦,易百教程官方技術(shù)學(xué)習(xí)群
注意:建議每個(gè)人選自己的技術(shù)方向加群,同一個(gè)QQ最多限加 3 個(gè)群。
總結(jié)
以上是生活随笔為你收集整理的java 复制文件_Java中复制文件的4种方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos 7 /etc/rc.loc
- 下一篇: java 多态性 接口_Java中的多态