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
/**
* Writes data from the input stream to the output stream.
*The input/output stream will be closed after the copy process is completed.
* @param in input stream
* @param os output stream
* @param bufferSize Data read buffer size (KB).
* @throws IOException If some input/output processing exception occurs
*/
public static void copyStream(InputStream in, OutputStream os,
int bufferSize) throws IOException {
int len = -1;
byte[] b = new byte[bufferSize * 1024];
try {
while ((len = in.read(b, 0, b.length)) != -1) {
os.write(b, 0, len);
}
os.flush();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
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(); } } }
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)