Reading a text file using spring batch where records are of different length i.e not in fixed length

998 views Asked by At

I having a text file having records as below

name(1-3),age(4-5),ownCar(6)(if has put Y else leave blank)

Samples of data :

Dom32Y

Pat22

Sam23

Rob45Y

So here if you see each record is not of fixed length in size..So how can i read this text file using Spring Batch. Now i am reading this file using FixedLengthTokenizer , But i am getting error "Caused by: org.springframework.batch.item.file.transform.IncorrectLineLengthException"

I'm new to Spring Batch and Java Config. If someone can help me writing a solution for my problem i would really aprecciate it!

1

There are 1 answers

1
Mahmoud Ben Hassine On

By default, the FixedLengthTokenizer works in strict mode, meaning it will not tolerate a line with more or less tokens than expected. You can relax that by setting the strict flag to false, which will behave as follows:

If false then lines with less tokens will be tolerated and padded with empty columns,
and lines with more tokens will simply be truncated.

If that is not what you are looking for, you can override FixedLengthTokenizer#doTokenize and provide your custom logic.