RESOLVED - issue BATCH-1317: Add flag to FlatFileItemWriter to delete empty files

This commit is contained in:
dsyer
2009-07-16 07:15:38 +00:00
parent 5bc4040efd
commit 758082e28f
3 changed files with 79 additions and 12 deletions

View File

@@ -77,6 +77,8 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
private boolean shouldDeleteIfExists = true;
private boolean shouldDeleteIfEmpty = false;
private String encoding = OutputState.DEFAULT_CHARSET;
private FlatFileHeaderCallback headerCallback;
@@ -134,12 +136,27 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
}
/**
* @param shouldDeleteIfExists the shouldDeleteIfExists to set
* Flag to indicate that the target file should be deleted if it already
* exists, otherwise it will be appended. If headers are emitted then
* appending will cause them to show up in the middle of the file. Defaults
* to true (so no appending except on restart).
*
* @param shouldDeleteIfExists the flag value to set
*/
public void setShouldDeleteIfExists(boolean shouldDeleteIfExists) {
this.shouldDeleteIfExists = shouldDeleteIfExists;
}
/**
* Flag to indicate that the target file should be deleted if no lines have
* been written (other than header and footer) on close. Defaults to false.
*
* @param shouldDeleteIfEmpty the flag value to set
*/
public void setShouldDeleteIfEmpty(boolean shouldDeleteIfEmpty) {
this.shouldDeleteIfEmpty = shouldDeleteIfEmpty;
}
/**
* Set the flag indicating whether or not state should be saved in the
* provided {@link ExecutionContext} during the {@link ItemStream} call to
@@ -220,10 +237,18 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
}
}
catch (IOException e) {
throw new ItemStreamException("Failed to writer footer before closing", e);
throw new ItemStreamException("Failed to write footer before closing", e);
}
finally {
getOutputState().close();
state.close();
if (state.linesWritten == 0 && shouldDeleteIfEmpty) {
try {
resource.getFile().delete();
}
catch (IOException e) {
throw new ItemStreamException("Failed to delete empty file on close", e);
}
}
state = null;
}
}
@@ -350,7 +375,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
}
outputBufferedWriter.flush();
pos = fileChannel.position() + ((TransactionAwareBufferedWriter)outputBufferedWriter).getBufferSize();
pos = fileChannel.position() + ((TransactionAwareBufferedWriter) outputBufferedWriter).getBufferSize();
return pos;
@@ -386,16 +411,33 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
initialized = false;
restarted = false;
try {
if (outputBufferedWriter == null) {
return;
if (outputBufferedWriter != null) {
outputBufferedWriter.close();
}
outputBufferedWriter.close();
fileChannel.close();
os.close();
}
catch (IOException ioe) {
throw new ItemStreamException("Unable to close the the ItemWriter", ioe);
}
finally {
try {
if (fileChannel != null) {
fileChannel.close();
}
}
catch (IOException ioe) {
throw new ItemStreamException("Unable to close the the ItemWriter", ioe);
}
finally {
try {
if (os != null) {
os.close();
}
}
catch (IOException ioe) {
throw new ItemStreamException("Unable to close the the ItemWriter", ioe);
}
}
}
}
/**

View File

@@ -61,7 +61,9 @@ public class FileUtils {
if (!overwriteOutputFile) {
throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
}
file.delete();
if (!file.delete()) {
throw new IOException("Could not delete file: "+file);
}
}
if (file.getParent() != null) {