Skip to content

Commit

Permalink
Update how the temp files are processed. Noticed the original was bei…
Browse files Browse the repository at this point in the history
…ng removed before the temp was renamed. This shoud be better.
  • Loading branch information
rbluer committed Oct 31, 2020
1 parent 4c1465b commit ca63f68
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/com/royalblueranger/mgpq/json/JacksonJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,30 +177,49 @@ public static void writeJsonFile(Path outputFile, Object obj)
{
ObjectMapper mapper = getJsonObjectMapper();

// Get a File object for both the original file and the new temp file.
File file = outputFile.toFile();
File renamedFile = null;
File tempFile = new File( file.getParentFile(), file.getName() + ".tmp");

try
{
// Write the data to the temp file
mapper.writeValue( tempFile, obj );

if ( file.exists() )
{
// the write should be completed and was successful; if it wasn't an exception would have been thrown

// Construct a the temp file name for the original file using it's last modified date:
long lastModified = file.lastModified();
Date lmDate = new Date(lastModified);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
String suffix = "_" + sdf.format( lmDate ) + ".json";
String fileName = file.getName().replace( ".json", suffix );
File renamedFile = new File( file.getParentFile(), fileName);

// Create a File object with the new temp file name for the original file
renamedFile = new File( file.getParentFile(), fileName);

// rename the original file
file.renameTo( renamedFile );

System.err.println( "JacksonJson.writeJsonFile: Existing file named to: " + renamedFile.getAbsolutePath() );
}

Files.deleteIfExists( outputFile );

tempFile.renameTo( file );

// If there was an original file, then delete the renamed version of it
if ( renamedFile != null && renamedFile.exists() ) {
boolean deleted = renamedFile.delete();

if ( !deleted ) {

System.err.println( "JacksonJson.writeJsonFile: Saved file failure: Could not remove the " +
"original file for an unknown reason. " + renamedFile.getAbsolutePath() );
}
}

System.err.println( "JacksonJson.writeJsonFile: Saved file: " + file.getAbsolutePath() );
}
catch ( IOException e )
Expand Down

0 comments on commit ca63f68

Please sign in to comment.