首页 文章

在spring-batch中使用FlatFileItemWriter创建新的输出文件

提问于
浏览
3

我有一个简单的 spring 批处理作业 - 逐行读取文件,对输入字符串执行某些操作,并写入一些输出 . 输出文件包含每行输入以及该行的某些处理状态(成功/失败 . )从以下位置读取文件: <dir>/<inputFolder>/<inputFileName> 并将处理后的输出写入 <dir>/<outputFolder>/<inputFileName> 所有这些值都作为 jobParameters 传递
文件阅读器是这样的:

<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>

项目编写器是这样的:

<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>

当我运行这个批处理作业时,读者正确读取文件,处理器完成其工作但itemWriter抛出了FileNotFound异常

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?

1 回答

  • 7

    添加'file://'前缀解决了我的问题 . 谢谢@LucaBassoRicci .

相关问题