java復制文件的4種方式


  java復制文件的4種方式
摘要
盡管Java提供了一個可以處理文件的IO操作類。 但是沒有一個復制文件的方法。 復制文件是一個重要的操作,當你的程序必須處理很多文件相關的時候。 然而有幾種方法可以進行Java文件復制操作,下面列舉出4中最受歡迎的方式。

1. 使用FileStreams復制

這是最經典的方式將一個文件的內容復制到另一個文件中。 使用FileInputStream讀取文件A的字節,使用FileOutputStream寫入到文件B。 這是第一個方法的代碼:


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

正如你所看到的我們執行幾個讀和寫操作try的數據,所以這應該是一個低效率的,下一個方法我們將看到新的方式。

2. 使用FileChannel復制

Java NIO包括transferFrom方法,根據文檔應該比文件流復制的速度更快。 這是第二種方法的代碼:


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

3. 使用Commons IO復制

Apache Commons IO提供拷貝文件方法在其FileUtils類,可用於復制一個文件到另一個地方。它非常方便使用Apache Commons FileUtils類時,您已經使用您的項目。基本上,這個類使用Java NIO FileChannel內部。 這是第三種方法的代碼:


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

4. 使用Java7的Files類復制

如果你有一些經驗在Java 7中你可能會知道,可以使用復制方法的Files類文件,從一個文件復制到另一個文件。 這是第四個方法的代碼:


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

5. 測試

現在看到這些方法中的哪一個是更高效的,我們會復制一個大文件使用每一個在一個簡單的程序。 從緩存來避免任何性能明顯我們將使用四個不同的源文件和四種不同的目標文件。 讓我們看一下代碼:


     
     
    
    
            
  1. import java.io. File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.nio.channels.FileChannel;
  8. import java.nio. file.Files;
  9. import org.apache.commons.io.FileUtils;
  10. public  class  CopyFilesExample {
  11. public   static   void  main(String[] args)  throws  InterruptedException,
  12. IOException {
  13. File  source =  new  File( "C:\\Users\\nikos7\\Desktop\\files\\sourcefile1.txt");
  14. File dest =  new  File( "C:\\Users\\nikos7\\Desktop\\files\\destfile1.txt");
  15. // copy file using FileStreams long  start = System.nanoTime();
  16. long end;
  17. copyFileUsingFileStreams( source, dest);
  18. System.out. println( "Time taken by FileStreams Copy = "
  19. + (System.nanoTime() - start));
  20. // copy files using java.nio.FileChannel source  =  new   File ( "C:\\Users\\nikos7\\Desktop\\files\\sourcefile2.txt" );
  21. dest =  new  File( "C:\\Users\\nikos7\\Desktop\\files\\destfile2.txt");
  22. start = System.nanoTime();
  23. copyFileUsingFileChannels( source, dest);
  24. end = System.nanoTime();
  25. System.out. println( "Time taken by FileChannels Copy = " + (end - start));
  26. // copy file using Java 7 Files class source  =  new   File ( "C:\\Users\\nikos7\\Desktop\\files\\sourcefile3.txt" );
  27. dest =  new  File( "C:\\Users\\nikos7\\Desktop\\files\\destfile3.txt");
  28. start = System.nanoTime();
  29. copyFileUsingJava7Files( source, dest);
  30. end = System.nanoTime();
  31. System.out. println( "Time taken by Java7 Files Copy = " + (end - start));
  32. // copy files using apache commons io source  =  new   File ( "C:\\Users\\nikos7\\Desktop\\files\\sourcefile4.txt" );
  33. dest =  new  File( "C:\\Users\\nikos7\\Desktop\\files\\destfile4.txt");
  34. start = System.nanoTime();
  35. copyFileUsingApacheCommonsIO( source, dest);
  36. end = System.nanoTime();
  37. System.out. println( "Time taken by Apache Commons IO Copy = "
  38. + (end - start));
  39. }
  40. private   static   void  copyFileUsingFileStreams( File   source File  dest)
  41. throws  IOException {
  42. InputStream input =  null;
  43. OutputStream output =  null;
  44. try {
  45. input =  new FileInputStream( source);
  46. output =  new FileOutputStream(dest);
  47. byte[] buf =  new  byte[ 1024];
  48. int bytesRead;
  49. while ((bytesRead = input. read(buf)) >  0) {
  50. output. write(buf,  0, bytesRead);
  51. }
  52. finally {
  53. input.close();
  54. output.close();
  55. }
  56. }
  57. private   static   void  copyFileUsingFileChannels( File   source File  dest)
  58. throws  IOException {
  59. FileChannel inputChannel =  null;
  60. FileChannel outputChannel =  null;
  61. try {
  62. inputChannel =  new FileInputStream( source).getChannel();
  63. outputChannel =  new FileOutputStream(dest).getChannel();
  64. outputChannel.transferFrom(inputChannel,  0, inputChannel. size());
  65. finally {
  66. inputChannel.close();
  67. outputChannel.close();
  68. }
  69. }
  70. private   static   void  copyFileUsingJava7Files( File   source File  dest)
  71. throws  IOException {
  72. Files. copy( source.toPath(), dest.toPath());
  73. }
  74. private   static   void  copyFileUsingApacheCommonsIO( File   source File  dest)
  75. throws  IOException {
  76. FileUtils.copyFile( source, dest);
  77. }
  78. }

輸出:


     
     
    
    
            
  1. Time taken  by FileStreams Copy =  127572360
  2. Time taken  by FileChannels Copy =  10449963
  3. Time taken  by Java7 Files Copy =  10808333
  4. Time taken  by Apache Commons IO Copy =  17971677

正如您可以看到的FileChannels拷貝大文件是最好的方法。如果你處理更大的文件,你會注意到一個更大的速度差。 這是一個示例,該示例演示了Java中四種不同的方法可以復制一個文件。

原文地址:https://blog.csdn.net/u014263388/article/details/52098719


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM