Java – read the newline character in CSV, which is referenced in the file in flatfileitemreader of spring batch

I tried to parse CSV files with flatfileitemreader This CSV contains some referenced line breaks, as shown below

email,name
abc@z.com,"NEW NAME
 ABC"

However, the parsing failed. The required field is 2, but the actual field is 1

What am I missing in the flatfilereader configuration?

<property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

                <!-- The lineTokenizer divides individual lines up into units of work -->
                <property name="lineTokenizer">
                    <bean
                        class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

                        <!-- Names of the CSV columns -->
                        <property name="names"
                            value="email,name" />
                    </bean>
                </property>

                <!-- The fieldSetMapper maps a line in the file to a Product object -->
                <property name="fieldSetMapper">
                    <bean
                        class="com.abc.testme.batchjobs.util.CustomerFieldSetMapper" />
                </property>
            </bean>
        </property>

Solution

The out of the box flatfileitemreader uses simplerecordseparatorpolicy as your use case

>The comment section exceeds 2 or more lines

You need to set defaultrecordseparatorpolicy

Javadoc referencing it:

Sample XML configuration

<bean id="reader" 
      class="org.springframework.batch.item.file.FlatFileItemReader">
      ...
    <property name="recordSeparatorPolicy">
        <bean class="org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy" />
    </property>
      ...
</bean>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>