How to skip empty fields reading a file using FlatFileItemReader?

I am reading a comma-delimited file that has two fields. The file may not contain a second field, so Spring DelimitedLineTokenizer should not complain when this happens. Stating the following

<property name="lineTokenizer"> <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"> <property name="names" value="planNumber, paymentAmount"> </property> <property name="delimiter"> <value>,</value> </property> </bean> </property> 

Spring complains

 Caused by: org.springframework.batch.item.file.transform.IncorrectTokenCountException: Incorrect number of tokens found in record: expected 2 actual 1 at org.springframework.batch.item.file.transform.AbstractLineTokenizer.tokenize(AbstractLineTokenizer.java:123) at org.springframework.batch.item.file.mapping.DefaultLineMapper.mapLine(DefaultLineMapper.java:46) ... 60 more 

StringTokenizer will not complain though

+4
source share
1 answer

set the linetokenizer property to the following false property: this should help avoid the throwing exception

 <property name="strict" value="false"></property> 
+7
source

Source: https://habr.com/ru/post/1401160/


All Articles