1

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.

Community
  • 1
  • 1
Java_m
  • 1
  • 5

2 Answers2

0

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.

Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
0

No.

At least because VM might be killed exactly after it left your code (after you entered File.rename() ), and before File.rename() code started running.

Yoni Roit
  • 28,280
  • 7
  • 36
  • 32