How to use spring batch to parse fixed length formats Files (File Without any delimiter)

巧了我就是萌 提交于 2021-02-07 18:27:28

问题


how to Configure spring-batch reader for fixed length formats Files (File Without any delimiter).

Each element is determined in relation to its starting and ending position.

Sample of line :

120180208FAILED
220180208SUCCES
120170208SUCCES
1 : code , 20180208 : date ,FAILED : status


回答1:


you can use FixedLengthTokenizer reader for this.

This is how you can configure FixedLengthTokenizer .

Sample Text file

UK21341EAH4121131.11customer1
UK21341EAH4221232.11customer2
UK21341EAH4321333.11customer3
UK21341EAH4421434.11customer4
UK21341EAH4521535.11customer5

Java Config

@Bean
    public FixedLengthTokenizer fixedLengthTokenizer() {
            FixedLengthTokenizer tokenizer = new FixedLengthTokenizer();

            tokenizer.setNames("ISIN", "Quantity", "Price", "Customer");
            tokenizer.setColumns(new Range(1-12),
                                 new Range(13-15),
                                 new Range(16-20),
                                 new Range(21-29));
            return tokenizer;
    }

XML Config

<bean id="fixedLengthLineTokenizer"
      class="org.springframework.batch.io.file.transform.FixedLengthTokenizer">
    <property name="names" value="ISIN,Quantity,Price,Customer" />
    <property name="columns" value="1-12, 13-15, 16-20, 21-29" />
</bean>



回答2:


When configuring the FixedLengthLineTokenizer, each of these lengths must be provided in the form of ranges:

<bean id="fixedLengthLineTokenizer"
      class="org.springframework.batch.io.file.transform.FixedLengthTokenizer">
    <property name="names" value="ISIN,Quantity,Price,Customer" />
    <property name="columns" value="1-12, 13-15, 16-20, 21-29" />
</bean>

visit https://docs.spring.io/spring-batch/trunk/reference/html/readersAndWriters.html#fixedLengthFileFormats



来源:https://stackoverflow.com/questions/48773303/how-to-use-spring-batch-to-parse-fixed-length-formats-files-file-without-any-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!