iluvAS iluvAS - 1 year ago 126
Java Question

Java: Unexpected exit during write

Lets say I have a java program writing out a large JSON taking up some time. Is there a way to determine if the program exits unexpectedly on the next startup to determine if my JSON is corrupted?

Answer Source

Rename the file after your process is complete:

    File outputFile = ...;
    someLongRunningProcess( outputFile );
    File successfulFile = ...
    outputFile.renameTo( successfulFile );
catch ( Exception ex )

If you don't have a successfulFile when you restart, your previous run wasn't successful.

Just keep the renameTo operation within a single file system so it's a simple, almost instantaneous atomic operation instead of any implied copy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download