Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6775895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:57:42+00:00 2026-05-26T15:57:42+00:00

I am writing a code in JDK 7 on Unix which compares two files.

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:57:43+00:00Added an answer on May 26, 2026 at 3:57 pm

    The safest approach that I can think of is to create the hard link with a temporary name in the destination directory and then, as the final step, rename it, thus overwriting the destination.

    Your basic algorithm would be something along the lines of the following snippet:

    if (FileContentComparator.same(FileA, FileB)) {
        Files.createLink(TempB, FileA);
        TempB.renameTo(FileB);
    }
    

    Renaming files within the same filesystem is usually atomic and, thus, reasonably safe. The use of a known pattern for the temporary names would allow them to be found and handled, even if the application terminates unexpectedly before performing the final rename.

    At least File.renameTo() will happily overwrite the destination without any hassle – you will have to investigate the behaviour of the Java NIO classes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I writing code that should compiled and run on both Windows and unix like
While writing code in a file that would comprise of PHP, HTML, CSS &
I'm writing code on the master page, and I need to know which child
I'm writing code which has a lot of 1 & 2 dimensional arrays. I
I am writing code to work with Amazon query based APIs, which return XML
I am writing code for uploading a file from a client to my server
I'm writing code to programmatically run git commands and learning git at the same
While writing code for connecting two webparts,I created a two labels. the text value
I was writing code for dragging mechanism which invokes to wait for small period
When writing code do you consciously program defensively to ensure high program quality and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.