Can we use Spring batch Item Reader and Writer on a file which needs to skip first and last line?

352 views Asked by At

I have a file to written to db after doing some validations. The file will have Header and Trailer which needs to be validated and then skipped, and all the lines in between should be mapped and loaded to a db if validations are met. Can I use Item Reader and Writer to do this? Below is a sample file data which has a header line, a trailer line and in between them the lines with actual data to be loaded to db. Any help is appreciated.

HEADER|xxxxx|20190405T143025Z linedata|linedata|linedata|linedata|||linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata|linedata

TRAILER|20190405T143025Z|1

p.s: I am an IIB Developer, this is my first time using spring batch.

1

There are 1 answers

3
Mahmoud Ben Hassine On BEST ANSWER

You can break down the requirement in two steps:

  • Step 1: a simple tasklet that does the validation logic (looks for header/trailer records and validates them). The success of this step is a pre-condition for the next step.
  • Step 2: a chunk-oriented tasklet that is triggered only if step 1 succeeds, and which skips the header with FlatFileItemReader.setLinesToSkip(1) and skips the trailer with a processor that filters records starting with TRAILER.