0

I am new to spring batch and need some help in parsing this file. I need to read this sample file where contents are between START_OF_FILE and END_OF_FILE tags. I need to read list of fields from START_OF_FIELDS and END_OF_FIELDS and then based on that read columns from START_OF_DATA and END_OF_DATA section. How can I read specific section of the file and read subsequent lines based on what was read from pervious section ? Thank you for your help in advance.

START_OF_FILE
STARRT_OF_FIELDS
COL2
COL3
END_OF_FIELDS
START_OF_DATA
COL1|COL2|COL3|COL4
COL1|COL2|COL3|COL4
COL1|COL2|COL3|COL4
COL1|COL2|COL3|COL4
COL1|COL2|COL3|COL4
END_OF_DATA
END_OF_FILE

1 Answers1

0

You need a custom ItemReader implementation for such use case. Your implementation could use the SingleItemPeekableItemReader to peek items ahead and detect the next line to be read. You can find a few examples here.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • Thank you @Mahmoud. I am new to and still learning spring batch. How can I get Field names separately and then data lines separately ? – TechieGirl May 07 '21 at 22:10
  • I would proceed in two steps: step1 (simple tasklet) does the necessary checks and calculations (detect fields, the line where acutal records start, etc) and a second step (chunk-oriented) which reads items line by line. Step 1 can pass infos to step 2 via the job execution context, see https://stackoverflow.com/questions/2292667. – Mahmoud Ben Hassine May 08 '21 at 17:36
  • Thank you again @Mahmoud. I will try this. – TechieGirl May 08 '21 at 22:47
  • Thanks again @Mahmoud. It is working as expected by using SingleItemPeekableItemReader – TechieGirl May 11 '21 at 21:39