FileChannel文件传输效率高,利用了操作系统的零拷贝进行优化,传输文件大小上限是2g,可以分批传输
public static void main(String[] args) {
try (
FileChannel from = new FileInputStream("data.txt").getChannel();
FileChannel to = new FileOutputStream("to.txt").getChannel();
) {
long size = from.size();
for (long left = size; left > 0; ) {
left -= from.transferTo((size - left), left, to);
}
} catch (Exception e) {
e.printStackTrace();
}
}