java文件复制方法_Java复制文件–用Java复制文件的4种方法

java文件复制方法

Java copy file is a very common operation. But java.io.File class doesn’t have any shortcut method to copy a file from source to destination. Here we will learn about four different ways we can copy file in java.

Java复制文件是非常常见的操作。 但是java.io.File类没有任何快捷方式将文件从源复制到目标。 在这里,我们将学习四种在java中复制文件的方法。

Java复制文件 (Java Copy File)

  1. Java Copy File – Stream

    Java复制文件–流

This is the conventional way of file copy in java. Here we create two Files – source and destination. Then we create InputStream from source and write it to the destination file using OutputStream for java copy file operation.

这是Java中文件复制的常规方式。 在这里,我们创建两个文件-源文件和目标文件。 然后,我们从源创建InputStream并使用OutputStream进行java复制文件操作,将其写入目标文件。

Here is the method that can be used for java copy file using streams.

这是可用于使用流的Java复制文件的方法。

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();
    }
}
  1. Java Copy File – java.nio.channels.FileChannel

    Java复制文件– java.nio.channels.FileChannel

Java NIO classes were introduced in Java 1.4 and FileChannel can be used to copy file in java. According to transferFrom() method javadoc, this way of copy file is supposed to be faster than using Streams for java copy files.

Java NIO类是在Java 1.4中引入的,FileChannel可用于在Java中复制文件。 根据transferFrom()方法javadoc,这种复制文件的方式应该比使用Streams进行Java复制文件要快。

Here is the method that can be used to copy a file using FileChannel.

这是可以使用FileChannel复制文件的方法。

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();
   }
}
  1. Java Copy File – Apache Commons IO FileUtils

    Java复制文件– Apache Commons IO FileUtils

Apache Commons IO FileUtils.copyFile(File srcFile, File destFile) can be used to copy file in java. If you are already using Apache Commons IO in your project, it makes sense to use this for code simplicity. Internally it uses Java NIO FileChannel, so you can avoid this wrapper method if you are not already using it for other functions.

Apache Commons IO FileUtilscopyFile(File srcFile,File destFile)可用于在Java中复制文件。 如果您已经在项目中使用Apache Commons IO,则使用它来简化代码是很有意义的。 它在内部使用Java NIO FileChannel,因此如果尚未将其用于其他功能,则可以避免使用该包装器方法。

Here is the method for using apache commons io for java copy file operation.

这是将apache commons io用于Java复制文件操作的方法。

private static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
    FileUtils.copyFile(source, dest);
}
  1. Java Copy File – Files class

    Java复制文件–文件类

If you are working on Java 7 or higher, you can use Files class copy() method to copy file in java. It uses File System providers to copy the files.

如果您使用的是Java 7或更高版本,则可以使用Files类的copy()方法在Java中复制文件。 它使用文件系统提供程序来复制文件。

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

Now to find out which is the fastest method, I wrote a test class and executed above methods one-by-one for copy file of 1 GB. In each call, I used different files to avoid any benefit to later methods because of caching.

现在,找出哪种方法最快,我编写了一个测试类,并针对1 GB的副本文件逐个执行了上述方法。 在每个调用中,我都使用了不同的文件,以避免由于缓存而对以后的方法产生任何好处。

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));        
    }
}

Here is the output of the above program, note that I commented above code to make sure every time only one method is used for java file copy operation.

这是上面程序的输出,请注意,我注释了上面的代码,以确保每次仅将一种方法用于Java文件复制操作。

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

From the output, it’s clear that Stream Copy is the best way to copy File in Java. But it’s a very basic test. If you are working on a performance intensive project, then you should try out different methods for java copy file and note down the timings to figure out the best approach for your project.

从输出中可以明显看出,Stream Copy是用Java复制File的最佳方法。 但这是一个非常基本的测试。 如果您正在处理一个性能密集型项目,则应尝试使用Java复制文件的不同方法,并记下时间安排,以找到适合您项目的最佳方法。

You should also play around different ways of java copy files based on your average size of the file.

您还应该根据文件的平均大小来尝试不同的Java复制文件方式。

I have created a YouTube video for 4 ways to copy the file in java, you can watch it to learn more.

我已经创建了一种YouTube视频,提供了4种方法来复制Java文件,您可以观看它以了解更多信息。

演示地址

翻译自: https://www.journaldev.com/861/java-copy-file

java文件复制方法

  • 18
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值