更新時間:2022-08-15 07:03:12 來源:動力節(jié)點 瀏覽8198次
Java文件復(fù)制的方法都有哪些?動力節(jié)點小編來為大家介紹以下四種Java文件復(fù)制的方法。
這是java中文件復(fù)制的常規(guī)方式。在這里,我們創(chuàng)建兩個文件 - 源和目標(biāo)。然后我們從源創(chuàng)建InputStream并使用OutputStream將其寫入目標(biāo)文件進(jìn)行 java 復(fù)制文件操作。這是可用于使用流的java復(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 類是在 Java 1.4 中引入的,F(xiàn)ileChannel 可用于在 java 中復(fù)制文件。根據(jù)transferFrom()方法 javadoc,這種復(fù)制文件的方式應(yīng)該比使用 Streams 復(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();
}
}
Apache Commons IO FileUtils。copyFile(File srcFile, File destFile)可用于在 java 中復(fù)制文件。如果您已經(jī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);
}
如果您正在使用 Java 7 或更高版本,則可以使用Files類的copy()方法在 java 中復(fù)制文件。它使用文件系統(tǒng)提供程序來復(fù)制文件。
private static void copyFileUsingJava7Files(File source, File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
現(xiàn)在為了找出最快的方法,編寫了一個測試類,并為 1 GB 的復(fù)制文件逐個執(zhí)行上述方法。在每次調(diào)用中,使用不同的文件來避免由于緩存而對后面的方法產(chǎn)生任何好處。
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));
}
}
這是上面程序的輸出,注意注釋了上面的代碼,以確保每次只使用一種方法進(jìn)行 java 文件復(fù)制操作。
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
從輸出中可以清楚地看出,Stream Copy 是在 Java 中復(fù)制 File 的最佳方式。但這是一個非常基本的測試。如果您正在處理性能密集型項目,那么您應(yīng)該嘗試不同的 java 復(fù)制文件方法并記下時間以找出適合您項目的最佳方法。您還應(yīng)該根據(jù)文件的平均大小嘗試不同的 java 復(fù)制文件方式。如果大家想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點的Java視頻教程,里面的課程內(nèi)容細(xì)致全面,通俗易懂,很適合沒有基礎(chǔ)的小伙伴學(xué)習(xí),希望對大家能夠有所幫助哦。
相關(guān)閱讀
初級 202925
初級 203221
初級 202629
初級 203743