I want to read transactions from text file and then write this transactions to database with JdbcBatchItemWriter. When I use csv file and DelimitedLineTokenizer, it works fine. If I use text file and FixedLengthTokenizer, couldn't read lines because readLine() method return null in doRead() method. How can I resolve this issue. Thanks for your help.
fileManager.xml
<bean id="transactionDataFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<property name="resource" value="#{jobParameters['processPath']}"/>
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.FixedLengthTokenizer">
<property name="names" value="type,code"/>
<!--<property name="columns" value="1-12, 13-15"/>-->
</bean>
</property>
<property name="fieldSetMapper">
<bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="transactionMap"/>
</bean>
</property>
</bean>
</property>
<property name="linesToSkip" value="1"/>
</bean>
<bean id="transactionDataFileItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource"/>
<property name="sql">
<value>
<![CDATA[
insert into TB_FORMAT(TYPE, CODE)
values (:type, :code)
]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider">
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider"/>
</property>
</bean>
file.txt
123 abc
456 def
try this - you have used DelimitedLineTokenizer , instead try using FixedLengthTokenizer
<bean id="transactionDataFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader"
scope="step">
<property name="resource"
value="#{jobParameters['processPath']}" />
<property name="lineMapper">
<bean
class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.io.file.transform.FixedLengthTokenizer">
<property name="names" value="type,code" />
<property name="columns" value="1-12, 13-15" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="transactionMap" />
</bean>
</property>
</bean>
</property>
<property name="linesToSkip" value="1" />
</bean>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With