Search code examples
javalinuxunixfile-renameatomic

File.rename() in Java - is it an atomic operation?


Possible Duplicate:
How to atomically rename a file in Java, even if the dest file already exists?

I'm working with a process the might be killed in middle of work. I want to rename a file. Is Java's rename operation an atomic operation?

I am interested in the case of Linux, HP-UX, Solaris, and AIX.


Solution

  • The Rename operation may or may not be atomic and it may or may not succeed if a file or directory already exists under the target name.On Unix two separate operations must be performed. First one must test whether a file exists under the target name. Then, if no such file exists, one can go ahead and rename the source file to the target name. Between these two operations a new file might be created under the target name and because the rename() system call is atomic and will destroy the target this new file will be deleted.