gpt4 book ai didi

java - 在 Spring Batch Flatfile Reader 中跳过页脚和页眉记录

转载 作者:行者123 更新时间:2023-12-02 04:50:01 26 4
gpt4 key购买 nike

我有一个 Spring 批处理作业来读取 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>

最佳答案

试试这个:

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());

关于java - 在 Spring Batch Flatfile Reader 中跳过页脚和页眉记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56451256/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com