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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27749755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:19:25  来源:igfitidea点击:

Files.move REPLACE_EXISTING cannot be resolved to a variable

javafilereplacemove

提问by phil294

The documentation of Files.move(Path source, Path target, CopyOption... options)says:

的文档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);

或者,假设我们要将文件移动到新目录,保持相同的文件名,并替换目录中该名称的任何现有文件:

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

Why do I get an error in the following code then?

为什么我在下面的代码中得到一个错误呢?

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

REPLACE_EXISTING cannot be resolved to a variable

REPLACE_EXISTING 无法解析为变量

回答by fge

You have to either write:

你必须要么写:

StandardCopyOption.REPLACE_EXISTING

or:

或者:

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

Note that you may also try and StandardCopyOption.ATOMIC_MOVEif you can

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

回答by Reimeus

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

回答by Alex

The documentation says its an argument of interface type java.nio.file.CopyOption, which has this implementation (an enum) that you're probably looking for: java.nio.file.StandardCopyOptionwhich has a definition for StandardCopyOption.REPLACE_EXISTING

文档说它是 interface type 的一个参数java.nio.file.CopyOption,它具有您可能正在寻找的这个实现(一个枚举):java.nio.file.StandardCopyOption它有一个定义StandardCopyOption.REPLACE_EXISTING