我正在编写代码,将一个文件的内容复制到另一个文件。
旧代码:
FileChannel source = new FileInputStream(inputFile).getChannel();
FileChannel destination = new FileOutputStream(outputFile).getChannel();
destination.transferFrom(source, 0, source.size());
此代码使我的程序冻结大约一分钟,以复制〜5MB的文件。仅使用常规的BufferedReader和PrintWriter即可正常工作。
此问题的解释是什么?是因为它试图在复制之前先将所有内容读取到内存中吗?