加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 读取CSV中的换行符,它在Spring批处理的FlatfileItemRead

发布时间:2020-12-15 00:53:54 所属栏目:Java 来源:网络整理
导读:我试图用FlatFileItemReader解析CSV文件.此CSV包含一些引用的换行符,如下所示. email,nameabc@z.com,"NEW NAME ABC" 但是这个解析失败了,必填字段为2但实际为1. 我在FlatFileReader配置中缺少什么? property name="lineMapper" bean class="org.springframe
我试图用FlatFileItemReader解析CSV文件.此CSV包含一些引用的换行符,如下所示.
email,name
abc@z.com,"NEW NAME
 ABC"

但是这个解析失败了,必填字段为2但实际为1.

我在FlatFileReader配置中缺少什么?

<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>

解决方法

开箱即用的FlatFileItemReader使用 SimpleRecordSeparatorPolicy作为您的用例

>评论部分超过2行或更多行

你需要设置DefaultRecordSeparatorPolicy

引用它的javadoc:

A RecordSeparatorPolicy that treats all lines as record endings,as
long as they do not have unterminated quotes,and do not end in a
continuation marker.

示例xml配置

<bean id="reader" 
      class="org.springframework.batch.item.file.FlatFileItemReader">
      ...
    <property name="recordSeparatorPolicy">
        <bean class="org.springframework.batch.item.file.separator.DefaultRecordSeparatorPolicy" />
    </property>
      ...
</bean>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读