RESOLVED - BATCH-1672: When appendAllowed is true, file is not created

(in the first time).
This commit is contained in:
Robert Kasanicky
2010-12-29 13:50:29 +01:00
parent 599891fd45
commit d094c18179
5 changed files with 82 additions and 47 deletions

View File

@@ -524,7 +524,7 @@ public class FlatFileItemWriter<T> extends ExecutionContextUserSupport implement
private void initializeBufferedWriter() throws IOException {
File file = resource.getFile();
FileUtils.setUpOutputFile(file, restarted || append, shouldDeleteIfExists);
FileUtils.setUpOutputFile(file, restarted, append, shouldDeleteIfExists);
os = new FileOutputStream(file.getAbsolutePath(), true);
fileChannel = os.getChannel();

View File

@@ -34,43 +34,51 @@ public final class FileUtils {
}
/**
* Set up output file for batch processing. This method implements common
* logic for handling output files when starting or restarting file I/O.
* When starting output file processing, creates/overwrites new file. When
* Set up output file for batch processing. This method implements common logic for handling output files when
* starting or restarting file I/O. When starting output file processing, creates/overwrites new file. When
* restarting output file processing, checks whether file is writable.
*
* @param file file to be set up
* @param restarted true signals that we are restarting output file
* processing
* @param overwriteOutputFile If set to true, output file will be
* overwritten (this flag is ignored when processing is restart)
* @param restarted true signals that we are restarting output file processing
* @param append true signals input file may already exist (but doesn't have to)
* @param overwriteOutputFile If set to true, output file will be overwritten (this flag is ignored when processing
* is restart)
*
* @throws IllegalArgumentException when file is null
* @throws ItemStreamException when starting output file processing, file
* exists and flag "overwriteOutputFile" is set to false
* @throws ItemStreamException when unable to create file or file is not
* writable
* @throws ItemStreamException when starting output file processing, file exists and flag "overwriteOutputFile" is
* set to false
* @throws ItemStreamException when unable to create file or file is not writable
*/
public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) {
public static void setUpOutputFile(File file, boolean restarted, boolean append, boolean overwriteOutputFile) {
Assert.notNull(file);
try {
if (!restarted) {
if (file.exists()) {
if (!overwriteOutputFile) {
throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
if (!append) {
if (file.exists()) {
if (!overwriteOutputFile) {
throw new ItemStreamException("File already exists: [" + file.getAbsolutePath() + "]");
}
if (!file.delete()) {
throw new IOException("Could not delete file: " + file);
}
}
if (!file.delete()) {
throw new IOException("Could not delete file: " + file);
}
}
if (file.getParent() != null) {
new File(file.getParent()).mkdirs();
if (file.getParent() != null) {
new File(file.getParent()).mkdirs();
}
if (!createNewFile(file)) {
throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() + "]");
}
}
if (!createNewFile(file)) {
throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() + "]");
else {
if (!file.exists()) {
if (!createNewFile(file)) {
throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath()
+ "]");
}
}
}
}
}
@@ -83,6 +91,13 @@ public final class FileUtils {
}
}
/**
* @deprecated use the version with explicit append parameter instead. Here append=false is assumed.
*/
public static void setUpOutputFile(File file, boolean restarted, boolean overwriteOutputFile) {
setUpOutputFile(file, restarted, false, overwriteOutputFile);
}
/**
* Create a new file if it doesn't already exist.
*

View File

@@ -361,7 +361,7 @@ public class StaxEventItemWriter<T> extends ExecutionContextUserSupport implemen
try {
file = resource.getFile();
FileUtils.setUpOutputFile(file, restarted, overwriteOutput);
FileUtils.setUpOutputFile(file, restarted, false, overwriteOutput);
Assert.state(resource.exists(), "Output resource must exist");
os = new FileOutputStream(file, true);
channel = os.getChannel();