Using custom delimiter while reading and writing batch file

In all previous Spring Batch posts, the DelimitedLineAggregator(which creates String format of java objects) and DelimitedLineTokenizer (which extracts fields from each record) assume ‘,’ as delimiter as shown below


We can use other symbols as delimiter and inform DelimitedLineAggregator and DelimitedLineTokenizer about this.

At the time of reading and writing, the default delimiter is ‘,’. We can change this as shown below


1   <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
2       <property name="resource" value="file:FileInput.txt"/>
3       <property name="lineMapper">
4           <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
5               <property name="lineTokenizer">
6                   <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
7                       <property name="delimiter" value="|"/>
8                       <property name="names" value="id,name,status,salary"/>
9                   </bean>
10              </property>
11              <property name="fieldSetMapper">
12                  <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
13                      <property name="prototypeBeanName" value="employee"/>
14                  </bean>
15              </property>
16          </bean>
17      </property> 
18  </bean>

In the reader xml code, the custom delimiter ‘|’ is specified at line 7. We set the delimiter property of DelimitedLineTokenizer class. In this way, we are
specifying that in the input file, the delimiter ‘|’ is used between the fields in the record as shown below



1   <bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
2       <property name="resource" value="file:FileOutput.txt"/>
3       <property name="lineAggregator">
4           <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
5               <property name="delimiter" value="|" />
6               <property name="fieldExtractor">
7                   <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
8                       <property name="names" value="id,name,status,salary"/>
9                   </bean>
10              </property>
11          </bean>
12      </property>
13  </bean>

In the writer xml code, the custom delimiter ‘|’ is specified at line 7. We set the delimiter property of DelimitedLineAggregator class. In this way, we are
specifying that in the output file,use the delimiter ‘|’ instead of the default ‘,’

Below is the complete xml code for your reference

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""
    <bean id="employee" class="package14.Employee" scope="prototype"/>
    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
        <property name="resource" value="file:FileInput.txt"/>
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
                <property name="lineTokenizer">
                    <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                        <property name="delimiter" value="|"/>
                        <property name="names" value="id,name,status,salary"/>
                <property name="fieldSetMapper">
                    <bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
                        <property name="prototypeBeanName" value="employee"/>
    <bean id="writer" class="org.springframework.batch.item.file.FlatFileItemWriter">
        <property name="resource" value="file:FileOutput.txt"/>
        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="delimiter" value="|" />
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="id,name,status,salary"/>
    <batch:job id="importProductsJob">
        <batch:step id="readWriteProducts">
                <batch:chunk reader="reader" writer="writer" commit-interval="50"/>
    <bean id="transactionManager" class="" />
    <bean id="jobRepository" class="">
        <property name="transactionManager" ref="transactionManager"/>
    <bean id="jobLauncher" class="">
        <property name="jobRepository" ref="jobRepository"/>

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s