在Spring Batch Flatfile Reader中跳过页脚和标题记录

时间:2019-06-04 20:57:12

标签: java spring-batch

我有一个春季批处理作业,可以读取CSV文件,该文件具有标题和尾部。要跳过页眉,只需跳过第一行,但是如何跳过页脚记录?

这是我要简化的读者:

<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
        scope="step">

        <property name="resource" value="file:#{jobParameters[file]}" />
        <property name="linesToSkip" value="1" />
        <property name="lineMapper">

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

                <property name="fieldSetMapper">

                    <bean class="ben.Mapper" />
                </property>

                <property name="lineTokenizer">

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

            </bean>

        </property>

    </bean>

1 个答案:

答案 0 :(得分:0)

尝试一下:

public class SkipFooterLineMapper extends DefaultLineMapper<XXX> {

    @Override
    public XXX mapLine(String line, int lineNumber) throws Exception {
        // skip the footer as footer like this: T~2952
        if(line != null && line.split("~").length == 2) {  
            return null;
        }
        return super.mapLine(line, lineNumber);
    }

}

FlatFileItemReader<XXX> bean = new FlatFileItemReader<>();
bean.setResource(new FileSystemResource(pathToFile));

// skip the header
bean.setLinesToSkip(1);
bean.setLineMapper(lineMapper());