Skip to content

Checking your reading list…

Retry

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

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.

File copy flowchart
File copy flowchart

1. Simple Stream Copy

Uses FileInputStream and FileOutputStream to read and write bytes. Suitable for text and binary files.

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();
    }
}

2. Buffered Stream Copy

Wraps the basic streams with BufferedInputStream and BufferedOutputStream to reduce I/O operations, improving speed 2–5×.

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();
    }
}

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.

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();
    }
}

4. FileChannel Transfer

Uses FileChannel’s transferTo or transferFrom methods, leveraging zero‑copy techniques for high performance, especially with large files.

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();
    }
}

5. RandomAccessFile with Memory‑Mapped Buffer

RandomAccessFile can map a file into memory via MappedByteBuffer, offering high efficiency for very large files.

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();
    }
}

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

JavaNIOFile I/OfilechannelBufferedStream

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion