Skip number of footer lines in Spring batch

6.3k views Asked by At

I am using FlatFileItemReader to read a delimited flat file. While I could skip number of headers with field linesToSkip, I wasn't able to skip footers by number of lines.

1

There are 1 answers

3
Santhosh Thamaraiselvan On BEST ANSWER

You can create custom line mapper, within which you can either skip by regex match or line number.

public class CustomLineMapper extends DefaultLineMapper<FieldSet> {
    @Setter
    private int totalItemsToRead;
    @Override
    public FieldSet mapLine(String line, int lineNumber) throws Exception {
        if(lineNumber > totalItemsToRead){
            return null;
        }
        return super.mapLine(line, lineNumber);
    }
}

Finally register the custom line mapper the FlatFileItemReader

CustomLineMapper lineMapper = new CustomLineMapper();
    lineMapper.setTotalItemsToRead(totalLinesInFile - numberOfLinesToSkipInFooter);

FlatFileItemReader<FieldSet> reader = new FlatFileItemReader<>();
// skip headers
reader.setLinesToSkip(linesToSkipInHeader);
// skip footer
reader.setLineMapper(lineMapper);