3

File.renameTo fails over NFS mounts so I'm using the commons.io FileUtils class's moveFile method. Occasionally it throws an IOException when moving a file however that file correctly shows up in the NFS mount. I'm assuming there is a race condition between when FileUtils checks to make sure the file was moved and NFS saying that's a valid file.

What is the best way to ensure a smooth file move over to an NFS mount in Java?

File f = new File("test.log");
FileUtils.moveFile(f, new File(newDir, f.getName));
James
  • 15,085
  • 25
  • 83
  • 120

1 Answers1

1
import static java.nio.file.StandardCopyOption.*;
import java.io.file.Files; 

Files.copy(source, target, REPLACE_EXISTING);

In Unix you can't rename or move between filesystems, so first you have to copy, and then if it was a move/rename, you would delete the source.

File f = ...;  
f.delete(); 
Chris K
  • 11,996
  • 7
  • 37
  • 65