• java复制文件的4种方式


     java复制文件的4种方式
    摘要
    尽管Java提供了一个可以处理文件的IO操作类。 但是没有一个复制文件的方法。 复制文件是一个重要的操作,当你的程序必须处理很多文件相关的时候。 然而有几种方法可以进行Java文件复制操作,下面列举出4中最受欢迎的方式。

    1. 使用FileStreams复制

    这是最经典的方式将一个文件的内容复制到另一个文件中。 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。 这是第一个方法的代码:

    1. private static void copyFileUsingFileStreams(File sourceFile 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 sourceFile 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 sourceFile dest)
    2.         throws IOException {
    3.     FileUtils.copyFile(source, dest);
    4. }

    4. 使用Java7的Files类复制

    如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。 这是第四个方法的代码:

    1. private static void copyFileUsingJava7Files(File sourceFile 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 FileStreamslong 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.FileChannelsource = 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 classsource = 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 iosource = 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 sourceFile 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 sourceFile 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 sourceFile dest)
    71. throws IOException {
    72. Files.copy(source.toPath(), dest.toPath());
    73. }
    74. private static void copyFileUsingApacheCommonsIO(File sourceFile 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
  • 相关阅读:
    GIL
    CRM2Stark组件
    Django图书管理系统(单表操作)
    Python(ATM机low版)
    Python(9-18天总结)
    Python(1-8天总结)
    Python习题(分页显示)
    Python文本操作2
    Python递归二分法
    Python文本操作
  • 原文地址:https://www.cnblogs.com/jpfss/p/11079757.html
Copyright © 2020-2023  润新知