Yesterday I wondered if the copyFile method in JTheque Utils was the best method or if I need to change. So I decided to do a benchmark.
So I searched all the methods to copy a File in Java, even the bad methods and found 5 methods :
- Native Copy : Make the copy using the cp executable of Linux
- Naive Streams Copy : Open two streams, one to read, one to write and transfer the content byte by byte.
- Naive Readers Copy : Open two readers, one to read, one to write and transfer the content character by character.
- Buffered Streams Copy : Same as the first but using buffered streams instead of simple streams.
- Buffered Readers Copy : Same as the second but using buffered readers instead of simple readers.
- Custom Buffer Stream Copy : Same as the first but reading the file not byte by byte but using a simple byte array as buffer.
- Custom Buffer Reader Copy : Same as the fifth but using a Reader instead of a stream.
- Custom Buffer Buffered Stream Copy : Same as the fifth but using buffered streams.
- Custom Buffer Buffered Reader Copy : Same as the sixth but using buffered readers.
- NIO Buffer Copy : Using NIO Channel and using a ByteBuffer to make the transfer.
- NIO Transfer Copy : Using NIO Channel and direct transfer from one channel to other.
- Path (Java 7) Copy : Using the Path class of Java 7 and its method copyTo()
I think, this is the principal methods to copy a file to another file. The different methods are available at the end of the post. Pay attention that the methods with Readers only works with text files because Readers are using character by character reading so it doesn’t work on a binary file like an image. Here I used a buffer size of 4096 bytes. Of course, use a higher value improve the performances of custom buffer strategies.
For the benchmark, I made the tests using different files.
- Little file (5 KB)
- Medium file (50 KB)
- Big file (5 MB)
- Fat file (50 MB)
- 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 :
- On the same hard disk. It’s an IDE Hard Disk of 250 GB with 8 MB of cache. It’s formatted in Ext4.
- 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.
- 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.
I’ve cut the post into several pages due to the length of the post :
- Introduction about the benchmark
- Benchmark on the same disk
- Benchmark between Ext4 and Ext4
- Benchmark between Ext4 and NTFS
- Conclusions about the benchmark results