Files.move REPLACE_EXISTING 无法解析为变量

Files.move REPLACE_EXISTING cannot be resolved to a variable

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

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

文档说它是接口类型 java.nio.file.CopyOption 的参数,它具有您可能正在寻找的此实现(枚举):java.nio.file.StandardCopyOption 具有 [=12= 的定义]

你必须写:

StandardCopyOption.REPLACE_EXISTING

或:

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

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