sonx
sonx

Reputation: 661

How to skip empty fields reading a file with FlatFileItemReader?

I'm reading a comma delimited file which has two fields. The file may not contain the second field at times so Spring DelimitedLineTokenizer should not complain when this happens. By 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 does complain

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 would not complain though

Upvotes: 6

Views: 9863

Answers (1)

Mahesh
Mahesh

Reputation: 345

set the following property on linetokenizer to false.. this should help avoid the exception getting thrown

<property name="strict" value="false"></property>

Upvotes: 10

Related Questions