我有两个 Java.io.File 对象 file1 和 file2。我想将文件1的内容复制到文件2。有没有一种标准方法可以做到这一点,而我不必创建一个读取 file1 并写入 file2 的方法
最佳答案
不,没有内置方法可以做到这一点。最接近您想要完成的是来自 FileOutputStream
的 transferFrom
方法,如下所示:
FileChannel src = new FileInputStream(file1).getChannel();
FileChannel dest = new FileOutputStream(file2).getChannel();
dest.transferFrom(src, 0, src.size());
不要忘记处理异常并关闭 finally
block 中的所有内容。
关于java IO将一个文件复制到另一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2520305/