Files.move REPLACE_EXISTING cannot be resolved to a variable

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

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);

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

1

3 Answers

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_MOVE if you can

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

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.StandardCopyOption which has a definition for StandardCopyOption.REPLACE_EXISTING

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like