使用 bufferedinputstream 和 bufferedoutputstream 配合固定大小的缓冲区(如 8192 字节)进行读写是 java 中复制大文件最快且最稳定的方式。通过减少系统调用次数并利用内存缓冲,可以显著提升 i/o 吞吐量。具体做法是创建一个足够大的 byte 数组作为缓冲区,循环读取直到返回 -1,并将每次读取的数据写入输出流。

为什么 BufferedStream 比原始 Stream 快
因为减少了操作系统层面的 read/write 系统调用次数。原始流每读一个字节或小块数据都会触发一次内核态切换,而缓冲流在内存中累积数据后一次性提交,大幅降低了上下文切换的开销。

import java.io.*;
public class FastCopy {
public static void main(String[] args) throws IOException {
String src = "large_file.bin";
String dest = "copy_large_file.bin";
long start = System.currentTimeMillis();
copyFile(src, dest);
long end = System.currentTimeMillis();
System.out.println("Time: " + (end - start) + " ms");
}
public static void copyFile(String srcPath, String destPath) throws IOException {
try (InputStream in = new FileInputStream(srcPath);
OutputStream out = new FileOutputStream(destPath)) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
}
}
预期结果:程序执行完毕后,目标文件大小与源文件一致,耗时显著低于未加缓冲的实现。对于几 GB 的文件,通常能达到磁盘顺序写满带宽的速度。
立即学习“Java免费学习笔记(深入)”;
FilePond 是一个 JavaScript 文件上传库,支持通过拖拽、粘贴和 API 方式处理文件、目录、URL 和 Data URI。它包含内置的图像优化功能、异步分块上传功能,并提供了适用于 React、Vue、Angular 和 Svelte 的适配器。
NIO FileChannel 是否更快
在大多数现代 JVM 和文件系统组合下,FileChannel.transferTo() 或 transferFrom() 利用零拷贝(Zero-Copy)技术,能进一步减少用户态与内核态之间的数据拷贝,理论上比 BufferedStream 更快,尤其是在大文件场景下。

import java.io.*;
import java.nio.channels.*;
public class NioFastCopy {
public static void main(String[] args) throws IOException {
String src = "large_file.bin";
String dest = "copy_nio_file.bin";
long start = System.currentTimeMillis();
copyFileNio(src, dest);
long end = System.currentTimeMillis();
System.out.println("NIO Time: " + (end - start) + " ms");
}
public static void copyFileNio(String srcPath, String destPath) throws IOException {
try (FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath)) {
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
}
}
}
预期结果:使用 NIO 方式复制相同大文件,耗时通常比 BufferedStream 低 10%-30%,具体取决于底层文件系统是否支持 sendfile 等零拷贝指令。若不支持,则回退到传统拷贝,性能接近 BufferedStream。
当文件超过 2GB 时 transferTo 会失败吗
是的,FileChannel.transferTo() 的参数类型为 long,但内部实现可能受限于平台或 JVM 版本对单次传输长度的限制。在某些旧版 JDK 或特定操作系统上,单次传输超过 2GB 可能导致异常或截断。此时应分块处理,确保每块大小不超过 Integer.MAX_VALUE。
import java.io.*;
import java.nio.channels.*;
public class SafeLargeFileCopy {
public static void main(String[] args) throws IOException {
String src = "very_large_file.bin";
String dest = "copy_safe_file.bin";
copySafe(src, dest);
}
public static void copySafe(String srcPath, String destPath) throws IOException {
try (FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath)) {
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
long position = 0;
long size = inChannel.size();
while (position < size) {
long remaining = size - position;
long transferred = inChannel.transferTo(position,
Math.min(remaining, Integer.MAX_VALUE), outChannel);
position += transferred;
}
}
}
}
预期结果:即使文件大于 2GB,也能完整复制。每次传输最多 2GB,避免溢出或平台限制导致的错误。这是生产环境中处理超大文件的推荐安全写法。

















