java - Files.move REPLACE_EXISTING 无法解析为变量

标签 java file replace move

Files.move(Path source, Path target, CopyOption... options) 的文档说:

Alternatively, suppose we want to move a file to new directory, keeping the same file name, and replacing any existing file of that name in the directory:

 Path source = ...
 Path newdir = ...
 Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);

为什么我会在下面的代码中出错?

 Files.move(Paths.get("outputFilePath"), Paths.get("inputFilePath"), REPLACE_EXISTING);

REPLACE_EXISTING cannot be resolved to a variable

最佳答案

你必须写:

StandardCopyOption.REPLACE_EXISTING

或:

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

请注意,如果可以的话,您也可以尝试 StandardCopyOption.ATOMIC_MOVE

关于java - Files.move REPLACE_EXISTING 无法解析为变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27749755/

相关文章:

bash - sed 在 bash 脚本中不起作用

java - Spring XML 架构问题

java - 当我尝试更新一行时,Android ListView 抛出 CurrentModificationException

java - Spring Cloud Feign 拦截器

c - 为什么这个功能不能与另一个功能一起使用?

Bash:获取文件类型的标准程序

Python 字符串替换不工作的新行

Java JVM 堆大小

c++ - DeleteFile() 或 CopyFile() 会抛出异常吗?

javascript - 如何删除 JavaScript location.pathname 返回值中的 "/"