Create a new output file using FlatFileItemWriter in spring-batch

I have a simple spring batch job - read the file line by line, do something with the input line and write some output. The output file contains each line of input plus some processing status for this line (success / failure.) Reading a file from: <dir>/<inputFolder>/<inputFileName>and writing processed output to <dir>/<outputFolder>/<inputFileName>All of these values ​​are transmitted as a jobParameters
file reader that looks like this:

<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="file:#{jobParameters['cwd']}/#{jobParameters['inputFolder']}/#{jobParameters['inputFile']}" />

        <property name="lineMapper">
          <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

            <property name="lineTokenizer">
              <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                <property name="delimiter" value="," />
              </bean>
            </property>
            <property name="fieldSetMapper" >
              <bean class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper" />
            </property>
          </bean>
        </property>
    </bean>

The item writer looks like this:

<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step" >
        <property name="resource" value="#{jobParameters['cwd']}/#{jobParameters['outputFolder']}/#{jobParameters['inputFile']}" />
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
        </property>
    </bean>  

When I run this batch job, the reader reads the file correctly, the processor does its job, but the FileNotFound exception is thrown by itemWriter

2014/06/27 18-02-31,168:OUT:ERROR[Encountered an error executing the step]
org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt]]
    at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:374)
    at org.springframework.batch.item.file.FlatFileItemWriter.open(FlatFileItemWriter.java:314)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
    Caused by: java.io.FileNotFoundException: class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt] cannot be resolved to URL because it does not exist
        at org.springframework.core.io.ClassPathResource.getURL(ClassPathResource.java:179)
        at org.springframework.core.io.AbstractFileResolvingResource.getFile(AbstractFileResolvingResource.java:48)
        at org.springframework.batch.item.file.FlatFileItemWriter.getOutputState(FlatFileItemWriter.java:371)
        ... 58 more
    2014/06/27 18-02-31,168:ERR:ERROR[Encountered an error executing the step]

    [org.springframework.batch.item.file.FlatFileItemWriter.getOutputState threw org.springframework.batch.item.ItemStreamException: Could not convert resource to file: [class path resource [S:/temp/seller-optin-batch/output/sellersToOptin_test.txt]]]
    Batch Execution Failed!

, , . ItemWriter . FlatFileItemWriter?

+3
1

'file://' . @LucaBassoRicci.

+6

Source: https://habr.com/ru/post/1546915/


All Articles