Files. Move Replace_Existing Cannot Be Resolved to a Variable

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

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.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.