Java Reference
In-Depth Information
org.springframework.batch.item.ItemStreamException: File already exists:
[/output/overwriteThisFile.txt]
at org.springframework.batch.item.util.FileUtils.setUpOutputFile(FileUtils.java:62)
at
org.springframework.batch.item.file.FlatFileItemWriter$OutputState.initialize
BufferedWriter(FlatFileItemWriter.java:497)
at
org.springframework.batch.item.file.FlatFileItemWriter$OutputState.access$000
(FlatFileItemWriter.java:354)
at
org.springframework.batch.item.file.FlatFileItemWriter.doOpen(FlatFileItemWri
ter.java:291)
at
org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWrite
r.java:281)
at
org.springframework.batch.item.support.CompositeItemStream.open(CompositeItem
Stream.java:98)
at
org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java
:288)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:193)
at
org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHan
dler.java:135)
at
org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowEx
ecutor.java:61)
The use of this parameter is a good idea in an environment where you want to preserve the output
of each run. This prevents an accidental overwrite of your old file.
The final option related to file creation is the appendAllowed parameter. When this flag (which
defaults to false ) is set to true , Spring Batch automatically sets the shouldDeleteIfExists flag to false ,
creates a new file if one doesn't exist, and appends the data if it does. This option can be useful if you
have an output file that you need to write to from multiple steps. Listing 9-17 shows formatJob
configured to append data if the file exists.
Listing 9-17. Appending Data if the Output File Exists
<beans:bean id="flatFileOutputWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter">
<beans:property name="resource" ref="outputFile"/>
<beans:property name="lineAggregator" ref="delimitedLineAggregator"/>
<beans:property name="appendAllowed" value="true"/>
</beans:bean>
With this configuration, you can run the job multiple times using the same output file (with different
input files), and Spring Batch appends the output of the current job to the end of the existing output file.
As you can see, there are a number of options available to handle flat file-based output, from being
able to format your records any way you want to generating delimited files and even providing options
 
 
Search WWH ::




Custom Search