1

I am using spring batch flatfile reader to read a file with header and footer values. Below is sample file and output file should have every record appended with header date and file sequence. Sample input and output as below. Can someone please suggest

  1. If there is a way to set the header values and footer values as job parameters and used in writer?
  2. Any way to get header and footer values in writer? Thanks in advance!
HH20210218001         --Header starting with "HH", date(20210218), filesequence(001)
name110
name220
name330
name440
name770
FT005                --Footer with "FT" and number of records

Output:(date + file seq(001) + name + age)

20210218001name110
20210218001name330
20210218001name440
20210218001name770

Below is my file reader

    <bean id="fileItemReader"
        class="org.springframework.batch.item.file.FlatFileItemReader"
        scope="step">
        <property name="resource" value="classpath:input/input.txt"></property>
        <!-- <property name="linesToSkip" value="2" /> -->
        <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="name,age" />
                        <property name="columns" value="1-5,6-8"></property>
                        <property name="strict" value="false" />

                    </bean>
                </property>
                <property name="fieldSetMapper">
                    <bean class="org.test.MyMapper">
                    </bean>
                </property>

            </bean>
        </property>

    </bean>

Teja
  • 69
  • 7

1 Answers1

0
  1. If there is a way to set the header values and footer values as job parameters and used in writer?

No, it's too late at that point. When the step starts, job parameters are already set and cannot be changed.

  1. Any way to get header and footer values in writer?

From your expected output, you don't need anything from the footer. If you want info from the header in your writer, you can do that in two steps:

  • step 1: reads (only) the header and puts any needed information in the execution context
  • step 2: uses a step-scoped writer that gets any required information from the execution context and uses it to write items
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50