[英]Copying files with Java
我尝试在替换旧文件时将文件从一个位置复制到另一个位置,但不断出现此错误: The method copy(Path, Path, CopyOption...) in the type Files is not applicable for the arguments (File, File, StandardCopyOption)
我的代码如下
Files.copy(file1, file2, StandardCopyOption.REPLACE_EXISTING);
我也尝试过使用这种方法:
CopyOption[] options = new CopyOption[]{
StandardCopyOption.REPLACE_EXISTING
};
Files.copy(file1, file2, options[0]);
我得到这个错误: The method copy(Path, Path, CopyOption...) in the type Files is not applicable for the arguments (File, File, CopyOption)
有什么想法吗?
您需要传递Path
对象,而不是File
:
Files.copy(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);
从有用的错误消息来看, file1
和file2
是File
对象。
但是您需要将Path
对象传递给copy
方法。
所以你需要使用
Files.copy(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);
代替。
您不应该使用文件。 而不是使用路径。
Files.copy(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.