I'm looking for the safest way to move a directory in Java. My code right now is basically
Path destPath = Paths.get(parentPath.toString().replace("service", "input"));
org.apache.commons.io.FileUtils.deleteQuietly(destPath.toFile());
org.apache.commons.io.FileUtils.moveDirectory(parentPath.toFile(), destPath.toFile());
Where parentPath is something like /dir/service/ and thus destPath is /dir/input/.
My problem is that while this approach works 99% of the time, in some cases it fails and deletes the files on the directory it was supposed to move. (It launches an IOException on the moveDirectory function claiming that it was not possible to delete the directory).
I need a way to make sure this problem is not going to happen (or at least my files would be safe), given that I cannot afford to lose this files.
I'm open to any new approaches too, I just need it to be safe and not too slow (given that my method is going to work with a high number of directories all the time).