iluvAS iluvAS - 9 months ago 79
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?


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.