Conclusion
In conclusion, the NIO Transfer method is the best one for big files but it’s not the fastest for little files (< 5 MB). But the custom buffer strategy (and the NIO Buffer too) are also really fast methods to copy files. We’ve also see that the method using the native utility tools to make the copy is faster as NIO for big files (< 1 GB) but it’s really slow for little files because of the cost of invoking an external program.
So perhaps, the best method is a method that make a custom buffer strategy on the little files and a NIO Transfer on the big ones and perhaps use the native executable on the really bigger ones. But it will be interesting to also make the tests on an other computer and operating system.
We can take several rules from this benchmark :
- Never made a copy of file byte by byte (or char by char)
- Prefer a buffer in your side more than in the stream to make less invocations of the read method, but don’t forget the buffer in the side of the streams
- Pay attention to the size of the buffers
- Don’t use char conversion if you only need to tranfer the content of a file, so don’t use Reader if you need only streams.
- Don’t hesitate to use channels to make file transfer, it’s the fastest way to make a file transfer.
- Consider the native executable invocation only for really bigger files.
- The new Path method of Java 7 is really fast except for the transfer of an enormous file between two disks.
I hope this benchmark (and its results) interested you.
Here are the sources of the benchmark : File Copy Benchmark Version 3
Here are the informations complete for the benchmark between two disks : Complete results of first two benchmarks