I have used Files.move method in my program as mentioned below.
public boolean moveAndRenameFile(String targetPath, String newName)
{
boolean fileMoved = true;
try
{
Path pathToFile = FileSystems.getDefault().getPath(targetPath);
Files.move(Paths.get(path), pathToFile.resolve(newName), StandardCopyOption.REPLACE_EXISTING);
}
catch (InvalidPathException | IOException e)
{
LOGGER.error("File couldn't be moved from {} to target location {}", path, targetPath);
LOGGER.error(e.getMessage(), e);
fileMoved = false;
}
return fileMoved;
}
Is it possible, that the file is deleted from original location but not moved to target location if any exception/error occurred in the middle?
I went through following link, but couldn't find answer for this question. https://docs.oracle.com/javase/tutorial/essential/io/move.html
The original (source) file won't get deleted until the process is completed. But an incomplete/corrupted file would be saved in the destination.
You can confirm this by doing a little test by yourself. Move a file to a removable disk and unplug the removable device before the process ends.
For the same storage provider it uses a native move.
Otherwise it does a
copyToForeignTarget(...);
Files.delete(source);
So there will not be a problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.