2

I have code that renames a file and then immediately attempts to open it.

On Windows XP this sometimes (but rarely) throws a FileNotFoundException.

The return value from renameTo is true.

Is this a known issue? Perhaps there can be a delay in the filesystem after a rename succeeds before the file actually appears?

Thanks

awinbra
  • 694
  • 1
  • 7
  • 19
  • 4
    What is the target filesystem (FAT, NTFS, SMB/CIFS, etc)? – NPE Jan 17 '12 at 19:51
  • When you check the actual file in your directories, has it's name been changed? – Alex Jan 17 '12 at 20:07
  • I don't know the filesyste, since this is a customers machine of an installed product, and all I have is a log file. – awinbra Jan 17 '12 at 20:23
  • I don't know if the actual file has had its name changed, I don't have access to the machine beyond log files. – awinbra Jan 17 '12 at 20:25
  • Most likely related to http://stackoverflow.com/questions/1000183/reliable-file-renameto-alternative-on-windows – Gray Jan 17 '12 at 22:11
  • It is puzzling. File renames should be atomic on NTFS. – dmeister Jan 18 '12 at 06:47
  • I think it is related to the fact that Java does [not (I think)](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/sun/nio/fs/WindowsFileCopy.java#393) add the [MOVEFILE_WRITE_THROUGH](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365240%28v=vs.85%29.aspx) flag to the MoveFileEx function (which is especially a problem if you move across different file systems). – eckes Jan 08 '15 at 16:26

1 Answers1

0

I have the same problem on Linux, moving files within a CIFS filesystem. The equivalent method Files.move(java.io.File, java.io.File) from the Google Guava library seems to not have this problem. What makes Guava better is that it explicitly copies the file from the source to the destination byte by byte if renameTo() doesn't return true. When I started writing this answer I didn't know that Guava delegated to File.renameTo() first, but still: no problems with Guava.

László van den Hoek
  • 3,955
  • 1
  • 23
  • 28