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.
Asked
Active
Viewed 5,701 times
2

Anandhan N
- 23
- 1
- 3
1 Answers
6
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);

Santhosh Thamaraiselvan
- 334
- 4
- 11
-
How to get the totalLinesInFile value. and what if I am using xml configuration – Rahul Sahu Mar 26 '19 at 05:00
-
Good catch. Thanks – Guilherme Jun 27 '21 at 21:16
-
For me, it didn't work with extending DefaultLineMapper, i had to extends PassThroughLineMapper. And after that, perfect, thank you ! – Jeff Jan 13 '23 at 16:46