Fastest way to copy files in Java

不想你离开。 提交于 2020-01-12 06:26:30

问题


What ist the fastest way to copy a big number of files in Java. So far I have used file streams and nio. Overall streams seem to be faster than nio. What experiences did you make so far?


回答1:


http://www.baptiste-wicht.com/2010/08/file-copy-in-java-benchmark/ might get you your answer.

For the benchmark, I made the tests using different files.

  1. Little file (5 KB)
  2. Medium file (50 KB)
  3. Big file (5 MB)
  4. Fat file (50 MB)
  5. And an enormous file (1.3 GB) only binary

And I made the tests first using text files and then using binary files. I made the tests using in three modes :

  1. On the same hard disk. It's an IDE Hard Disk of 250 GB with 8 MB of cache. It's formatted in Ext4.
  2. Between two disk. I used the first disk and an other SATA Hard Disk of 250 GB with 16 MB of cache. It's formatted in Ext4.
  3. Between two disk. I used the first disk and an other SATA Hard Disk of 1 TB with 32 MB of cache. It's formatted using NTFS.

I used a benchmark framework, described here, to make the tests of all the methods. The tests have been made on my personal computer (Ubuntu 10.04 64 bits, Intel Core 2 Duo 3.16 GHz, 6 Go DDR2, SATA Hard Disks). The Java version used is a Java 7 64 bits Virtual Machine...




回答2:


I would use:

import java.io.*;
import java.nio.channels.*;

public class FileUtils{
    public static void copyFile(File in, File out) 
        throws IOException 
    {
        FileChannel inChannel = new
            FileInputStream(in).getChannel();
        FileChannel outChannel = new
            FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(),
                    outChannel);
        } 
        catch (IOException e) {
            throw e;
        }
        finally {
            if (inChannel != null) inChannel.close();
            if (outChannel != null) outChannel.close();
        }
    }

    public static void main(String args[]) throws IOException{
        FileUtils.copyFile(new File(args[0]),new File(args[1]));
  }
}

If any of your files are bigger than 64M in Windows you might need to look at this: http://forums.sun.com/thread.jspa?threadID=439695&messageID=2917510




回答3:


Have java fork off an OS batch script that copies the files. Your code might have to write the batch script.




回答4:


You can use either FileUtils implementation of apache commons-io library to copy file

FileUtils.copyFile(new File(sourcePath), new File(destPath));

Which uses FileChannel for IO operation.

Or use java.nio.file.Files's copy() method.




回答5:


Its depend of files(bigger files), for me is fastest way this one with buffered stream

 public void copyFile(File inFileStr, File outFileStr) throws IOException {

    try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(inFileStr)); 
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outFileStr))) {

        byte[] buffer = new byte[1024 * 1024];
        int read = 0;
        while ((read = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, read);
        }

        bis.close();
        bos.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}


来源:https://stackoverflow.com/questions/4004760/fastest-way-to-copy-files-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!