Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip empty fields reading a file with FlatFileItemReader?

Tags:

spring-batch

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

like image 819
sonx Avatar asked Mar 13 '12 11:03

sonx


People also ask

When does the fileitemreader return NULL or empty?

By default, the FileItemReader will return null signaling that the step is complete when it encounters an empty file (assuming that the file exists. If it doesn't, we're talking about a ... 4.

Does flatfileitemreader trim the leading and trailing spaces automatically?

Issue#FlatFileItemReader automatically Trimming the leading and trailing spaces forum.springsource.org Hi, FlatFileItemReader trims the leading and trailing spaces automaticaly. How to restrict it.

How do I know if a file is empty or not?

If the file size is greater than zero, the script reads lines from the file until there are no more lines, or until the number of lines exceeds the expected number of non-data rows. If the number of lines in the file is less than or equal to the expected number of non-data rows, then the file is considered empty.

Does the flat file source determine whether a flat file contains data?

The Flat File source does not determine whether a flat file contains rows of data before attempting to process it. You may want to improve the efficiency of a package, especially of a package that iterates over numerous flat files, by skipping files that do not contain any rows of data.


1 Answers

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

<property name="strict" value="false"></property>
like image 76
Mahesh Avatar answered Sep 20 '22 14:09

Mahesh