繁体   English   中英

用Java复制文件

[英]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);

从有用的错误消息来看, file1file2File对象。

但是您需要将Path对象传递给copy方法。

所以你需要使用

Files.copy(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);

代替。

您不应该使用文件。 而不是使用路径。

Files.copy(file1.toPath(), file2.toPath(), StandardCopyOption.REPLACE_EXISTING);

您是否尝试过投放StandardCopyOption?

尝试:

    CopyOption co = (CopyOption) StandardCopyOption.REPLACE_EXISTING;

基于您收到的错误,在我看来,即使方法Files.copy()使用Inputstream或Path,而不是Files,您也​​在使用文件。

这可能是Java复制文件的副本, 并替换了现有目标

希望这可以帮助。 祝你好运:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM