B.Nbl
B.Nbl

Reputation: 616

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

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

Upvotes: 5

Views: 13519

Answers (2)

Niraj Sonawane
Niraj Sonawane

Reputation: 11075

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>

Upvotes: 9

Zyos
Zyos

Reputation: 11

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

Upvotes: 1

Related Questions