MENU

Java file copy (change buffer size)

TOC

Java file copy (change buffer size)

 In Java InputStream, ,OutputStream at
Introducing a sample program that performs copy processing using input/output streams.
In the copy process using streams, even large files can be copied relatively quickly by increasing the read buffer size.
In the sample program, the buffer size for reading data can be specified as an argument.

 Since the specified read buffer area is allocated in the JavaVM heap, it is inefficient to make it too large. Therefore, we also check for an efficient data read buffer size when copying files.

sample program


execution (e.g. program)

 A 100MB file "100M.txt" is prepared directly under the C drive for file copying of the sample program. In the sample program, C:\100M.txt and read data in units of 1000KB buffer size C:\a.txt Copy to

The sample was run in the following environment.
OS : WindowsXP
CPU : Athlon 1.46GHz
Memory : 1GB
JRE : 1.6.4

◆Example of Execution

/**
 * Execution example
 * @param args
 */
public static void main(String[] args) {
    try {
        copyStream("C:\\100M.txt", "C:\\a.txt",1000);
    } catch (IOException e) {
        e.printStackTrace(); }
    }
}

◆Execution Result


Change buffer size

To check the efficient data read buffer size when copying files, use the third argument of copyStream as
The results of similar copy processing in 1KB, 100KB, 1000KB, and 10000KB units are summarized below.



Although it cannot be concluded from the above results alone, if you specify the data read buffer size in units of 1000KB,
The results showed that it was efficient in terms of heap usage and processing time during copy processing.
Similar results were obtained when the file size was increased (approximately 1GB).

On the other hand, if you are copying a small file (1MB or less), it is better to specify a buffer size of 100KB or so, since a large buffer size will be useless.

Use FileChannel for more speed.


 InputStream, ,OutputStream
Copy processing is important in understanding input/output streams, but
New I/O introduced in J2SE1.4 FileChannel#transferTo
method can be used to copy files even more efficiently.
Next, we will show you how to use FileChannel for easy and fast file copying.
Java File Copy (easy and fast)

TOC