Boost Java File Copy Performance: 5 Proven Methods Compared
Learn five Java file copy techniques—from basic streams to NIO Files.copy, FileChannel, and RandomAccessFile—detailing their implementations, performance differences, and ideal use cases for small, large, and massive files, helping you choose the most efficient method for your project.
Overview
File copying is a common operation in Java for backup, migration, or handling large data sets. This article presents five typical ways to copy files.
1. Simple Stream Copy
Uses FileInputStream and FileOutputStream to read and write bytes. Suitable for text and binary files.
<code>public static void main(String[] args) {
try (InputStream inputStream = new FileInputStream("my_test.txt")) {
OutputStream outputStream = new FileOutputStream("receive.txt");
byte[] bytes = new byte[1024];
int size;
while ((size = inputStream.read(bytes)) > 0) {
outputStream.write(bytes, 0, size);
}
} catch (IOException e) {
e.printStackTrace();
}
}</code>2. Buffered Stream Copy
Wraps the basic streams with BufferedInputStream and BufferedOutputStream to reduce I/O operations, improving speed 2–5×.
<code>public static void main(String[] args) {
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("my_test.txt"));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream("receive.txt"))) {
int size;
byte[] bytes = new byte[1024];
while ((size = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, size);
}
} catch (IOException e) {
e.printStackTrace();
}
}</code>3. NIO Files.copy
The Files.copy method from java.nio.file provides a concise way to copy files, with options such as REPLACE_EXISTING, COPY_ATTRIBUTES, and ATOMIC_MOVE.
<code>public static void main(String[] args) {
Path source = Paths.get("my_test.txt");
Path target = Paths.get("receive.txt");
try {
Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}</code>4. FileChannel Transfer
Uses FileChannel’s transferTo or transferFrom methods, leveraging zero‑copy techniques for high performance, especially with large files.
<code>public static void main(String[] args) {
try {
FileChannel sourceChannel = new FileInputStream("my_test.txt").getChannel();
FileChannel targetChannel = new FileOutputStream("receive.txt").getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
} catch (IOException e) {
e.printStackTrace();
}
}</code>5. RandomAccessFile with Memory‑Mapped Buffer
RandomAccessFile can map a file into memory via MappedByteBuffer, offering high efficiency for very large files.
<code>public static void main(String[] args) {
try (RandomAccessFile sourceFile = new RandomAccessFile("my_test.txt", "r");
RandomAccessFile receiveFile = new RandomAccessFile("receive.txt", "rw")) {
FileChannel sourceChannel = sourceFile.getChannel();
MappedByteBuffer byteBuffer = sourceChannel.map(FileChannel.MapMode.READ_ONLY, 0, sourceChannel.size());
receiveFile.getChannel().write(byteBuffer);
} catch (IOException e) {
e.printStackTrace();
}
}</code>Comparison of Methods
Simple Stream : Best for small files (<10 MB).
Buffered Stream : General purpose, works well in most scenarios.
Files.copy : Simple code, good for quick development.
FileChannel : Ideal for large files (>100 MB) due to zero‑copy.
RandomAccessFile : Suited for very large files (>1 GB) with memory‑mapping.
Choosing the appropriate copy method depends on file size and performance requirements; for large or massive files, NIO‑based approaches generally outperform basic byte‑stream copying.
Lobster Programming
Sharing insights on technical analysis and exchange, making life better through technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.