I am writing a code in JDK 7 on Unix which compares two files. If both files are same say File A and File B.
Then it should delete File B and create a hardlink to File A.
Simple way is :
1. Compare if files are same
a. delete File B
b. use Path to File B to createLink to File A
But the problem is this is not atomic. So for some reason if my Java code dies after step 1 . I have lost the file.
One Solution is to create backup file and clean it later if process executes fine.
But I am looking for more elegant solution , something in which I can do this as a atomic operation . Please help .
Thanks