4種java復制文件的方法。本站提示廣大學習愛好者:(4種java復制文件的方法)文章只能為提供參考,不一定能成為您想要的結果。以下是4種java復制文件的方法正文
雖然Java供給了一個可以處置文件的IO操作類,然則沒有一個復制文件的辦法。復制文件是一個主要的操作,當你的法式必需處置許多文件相干的時刻。但是有幾種辦法可以停止Java文件復制操作,上面羅列出4中最受迎接的方法。
1. 應用FileStreams復制
這是最經典的方法將一個文件的內容復制到另外一個文件中。 應用FileInputStream讀取文件A的字節,應用FileOutputStream寫入到文件B。 這是第一個辦法的代碼:
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(); } }
正如你所看到的我們履行幾個讀和寫操作try的數據,所以這應當是一個低效力的,下一個辦法我們將看到新的方法。
2. 應用FileChannel復制
Java NIO包含transferFrom辦法,依據文檔應當比文件流復制的速度更快。這是第二種辦法的代碼:
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(); } }
3. 應用Commons IO復制
Apache Commons IO供給拷貝文件辦法在其FileUtils類,可用於復制一個文件到另外一個處所。它異常便利應用Apache Commons FileUtils類時,您曾經應用您的項目。根本上,這個類應用Java NIO FileChannel外部。 這是第三種辦法的代碼:
private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { FileUtils.copyFile(source, dest); }
4. 應用Java7的Files類復制
假如你有一些經歷在Java 7中你能夠會曉得,可使用復制辦法的Files類文件,從一個文件復制到另外一個文件。 這是第四個辦法的代碼:
private static void copyFileUsingJava7Files(File source, File dest) throws IOException { Files.copy(source.toPath(), dest.toPath()); }
5. 測試
如今看到這些辦法中的哪個是更高效的,我們會復制一個年夜文件應用每個在一個簡略的法式。 從緩存來防止任何機能顯著我們將應用四個分歧的源文件和四種分歧的目的文件。 讓我們看一下代碼:
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 CopyFilesExample { public static void main(String[] args) throws InterruptedException, IOException { File source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt"); File dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt"); // copy file using FileStreams long start = System.nanoTime(); long end; copyFileUsingFileStreams(source, dest); System.out.println("Time taken by FileStreams Copy = " + (System.nanoTime() - start)); // copy files using java.nio.FileChannel source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt"); dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt"); start = System.nanoTime(); copyFileUsingFileChannels(source, dest); end = System.nanoTime(); System.out.println("Time taken by FileChannels Copy = " + (end - start)); // copy file using Java 7 Files class source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt"); dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt"); start = System.nanoTime(); copyFileUsingJava7Files(source, dest); end = System.nanoTime(); System.out.println("Time taken by Java7 Files Copy = " + (end - start)); // copy files using apache commons io source = new File("C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt"); dest = new File("C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt"); start = System.nanoTime(); copyFileUsingApacheCommonsIO(source, dest); end = System.nanoTime(); System.out.println("Time taken by Apache Commons IO Copy = " + (end - 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); } }
輸入:
Time taken by FileStreams Copy = 127572360 Time taken by FileChannels Copy = 10449963 Time taken by Java7 Files Copy = 10808333 Time taken by Apache Commons IO Copy = 17971677
正如您可以看到的FileChannels拷貝年夜文件是最好的辦法。假如你處置更年夜的文件,你會留意到一個更年夜的速度差。這是一個示例,該示例演示了Java中四種分歧的辦法可以復制一個文件。
以上就是本文的全體內容,願望對年夜家的進修有所贊助。