Question

i would like to know which is the best way to copy large number of files.

Was it helpful?

Solution

If you're copying files locally, you should use FileChannel.transferFrom() or FileChannel.transferTo(). For example:

FileInputStream is = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(oFile);
FileChannel f = is.getChannel();
FileChannel f2 = fos.getChannel();

f.transferTo(0, f.size(), f2);

f2.close();
f.close();

On many platforms, the copying will happen via DMA and be about as fast as possible.

If you're copying files over a network, then you're in a different situation. In the case that some of the files might already exist, then you should look into rsync, as it can avoid transferring parts of files which are the same on both sides. If the most common case is that the files won't already exist on the destination machine, then you'll want to use something like scp. For that, see this question.

OTHER TIPS

"Best" needs clarification.

I would delegate to rsync which is very good at copying a large number of files with a lot of options. I am not aware of a good Java implementation of rsync.

Check out the Files class from Google's guava library. It contains some utility methods for copying whole files.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top