Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileAlreadyExistsException with REPLACE_EXISTING option

Inside my code there is a loop to substitute a file with another file.

This is done with:

java.nio.file.Files.move(Path source, Path target, CopyOption... options) throws IOException

The following exception is thrown:

Exception in thread "main" java.nio.file.FileAlreadyExistsException: C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat.temp -> C:\BRUTE-FORCE\Test-Loads-2-forces-only.dat
    at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
    at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
    at sun.nio.fs.WindowsFileCopy.move(Unknown Source)
    at sun.nio.fs.WindowsFileSystemProvider.move(Unknown Source)
    at java.nio.file.Files.move(Unknown Source)
(*) at bruteforce.Main.changeValue(Main.java:260)
    at bruteforce.Main.main(Main.java:71)

The line at which the exception is thrown:

(*) at bruteforce.Main.changeValue(Main.java:260):

Files.move(path, path.resolveSibling("DESTINY_FILE"), REPLACE_EXISTING);

Javadoc defines the exception:

...
FileAlreadyExistsException - if the target file exists but cannot be replaced because the REPLACE_EXISTING option is not specified (optional specific exception) 
...

The code clearly specifies REPLACE_EXISTING.

Also the option is imported at the beginning of the file:

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

Any hint?

It could be due to the computer (or the HDD) hybernates or something similar? I have set the enegy options not suspends while using the power grid.

Thanks in advance

like image 611
J Robes Avatar asked Apr 19 '16 08:04

J Robes


People also ask

What is filealreadyexistsexception in Java?

java.nio.file.FileAlreadyExistsException. All Implemented Interfaces: Serializable. public class FileAlreadyExistsException extends FileSystemException. Checked exception thrown when an attempt is made to create a file or directory and a file of that name already exists. Since: 1.7. See Also: Serialized Form.

What is the use of the Replace option?

The replace option specifies whether to overwrite existing files on your workstation, or to prompt you for your selection when you restore or retrieve files. Important: The replace option does not affect recovery of directory objects.

Can I replace locked files when the system is rebooted?

Note: You can choose to replace locked files when the system is rebooted. The client cannot perform an in-place restore of active files. However, it stages restored versions of active files for replacement during the next reboot, except for files containing named streams, sparse files, and directories.


1 Answers

Files.move is not an atomic operation (unless of course ATOMIC_MOVE is specified), so what i assume is happenig some other IO got lock on this file.

Please make sure you:

  • Lunch close() method on this resource or use try-with-resources

  • Your OS is not using this file (eg. opened in Notepad, you lunch tail against it)

If you file is often accesed, then you can try to create loop that checks Files.isWritable()

like image 194
Wild_Owl Avatar answered Nov 08 '22 12:11

Wild_Owl