public class FileUtil {
/**
* 复制文件
* @param from 原文件
* @param to 目标文件
*/
public static void copyFile(String from, String to)
throws FileNotFoundException, IOException {
FileChannel
fChannel = new FileInputStream(from).getChannel(),
tChannel = new FileOutputStream(from).getChannel();
fChannel.transferTo(0, fChannel.size(), tChannel);
//或者tChannel.transferFrom(fChannel, 0, fChannel.size());
}
}
/**
* 复制文件
* @param from 原文件
* @param to 目标文件
*/
public static void copyFile(String from, String to)
throws FileNotFoundException, IOException {
FileChannel
fChannel = new FileInputStream(from).getChannel(),
tChannel = new FileOutputStream(from).getChannel();
fChannel.transferTo(0, fChannel.size(), tChannel);
//或者tChannel.transferFrom(fChannel, 0, fChannel.size());
}
}
本文介绍了一种使用Java实现文件复制的方法。通过FileChannel的transferTo或transferFrom方法完成文件内容的高效复制。此方法避免了传统字节流逐字节读写带来的性能开销。

被折叠的 条评论
为什么被折叠?



