Delete the temp file even if close() throws an exception
@@ -119,8 +119,7 @@ public class AtomicFileWriter extends Writer {
* the {@link #commit()} is called, to simplify coding.
*/
public void abort() throws IOException {
- close();
- Files.deleteIfExists(tmpPath);
+ closeAndDeleteTempFile();
}
public void commit() throws IOException {
@@ -157,10 +156,17 @@ public class AtomicFileWriter extends Writer {
@Override
protected void finalize() throws Throwable {
+ closeAndDeleteTempFile();
+ }
+
+ private void closeAndDeleteTempFile() throws IOException {
// one way or the other, temporary file should be deleted.
+ try {
close();
+ } finally {
Files.deleteIfExists(tmpPath);
}
+ }
/**
* Until the data is committed, this file captures
