Spring batch: Writing column names as first line in flat file

Vicky picture Vicky · Sep 12, 2012 · Viewed 25k times · Source

I want to create a flat file which has the below format:

Col1Name;Col2Name;Col3Name
one;23;20120912
two;28;20120712

As seen, the first line in the flat file are the column names.

How to achieve this through header callback ?

I see that if the input file is of above format, there is an option as below to ignore first line:

<property name="firstLineIsHeader" value="true"/>

Also, this Jira Issue indicates that what I want is implemeted and closed. However, I am unable to find any example for writing first line as column names.

<beans:bean id="MyFileItemWriter" class="com.nik.MyFileItemWriter" scope="step">
    <beans:property name="delegate">
        <beans:bean class="org.springframework.batch.item.file.FlatFileItemWriter">
            <beans:property name="resource" value="file:MYFILE.dat" /> 

            <beans:property name="lineAggregator">
                <beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                    <beans:property name="delimiter" value=";" />
                    <beans:property name="fieldExtractor">
                        <beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                            <beans:property name="names" value="Col1Name, Col2Name, Col3Name" />
                        </beans:bean>
                    </beans:property>
                </beans:bean>
            </beans:property>
            <beans:property name="headerCallback" ref="MyFileItemWriter" />
        </beans:bean>
    </beans:property>
</beans:bean>

My Item Writer looks as below:

public class MyFileItemWriter implements ItemWriter<MyBean>, FlatFileHeaderCallback, ItemStream{

private FlatFileItemWriter<MyBean> delegate;    

 public void setDelegate(final FlatFileItemWriter<MyBean> delegate) {
        this.delegate = delegate;
    }

public void writeHeader(Writer writer) throws IOException {


}

public void write(List<? extends MyBean> items) throws Exception {
    this.delegate.write(items);

}

public void close() throws ItemStreamException {
     this.delegate.close();

}

public void open(ExecutionContext arg0) throws ItemStreamException {
     this.delegate.open(arg0);

}

public void update(ExecutionContext arg0) throws ItemStreamException {
     this.delegate.update(arg0);        
}

}

Thanks for reading!

Answer

Nather Webber picture Nather Webber · May 29, 2014

create a custom class which extends the FlatFileItemWriter and implements just the constructor:

public class MyFlatFileWriter extends FlatFileItemWriter {

    public MyFlatFileWriter (){
        super.setHeaderCallback(new FlatFileHeaderCallback() {

            public void writeHeader(Writer writer) throws IOException {
                writer.write("Col1Name,Col2Name,Col3Name");

            }
        });
    }

and then use this class in the bean configuration class attribute