10

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!

Vicky
  • 16,679
  • 54
  • 139
  • 232

3 Answers3

17

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

6

well did you try to work with

  public void writeHeader(Writer writer) throws IOException {
      //... e.g. writer.write("my first line");

  } 
Michael Pralow
  • 6,560
  • 2
  • 30
  • 46
  • Yes, I could do that. But I thought that would be synonymous to hard coding. Instead, I thought there would be a way present to list down the header directly from bean property names automatically. So want to confirm that there is no such functionality available. If not, I will go with the above way. – Vicky Sep 13 '12 at 04:50
  • to minimize the hard coding you could re-use the attribute name list with e.g. http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-util-list – Michael Pralow Sep 13 '12 at 10:31
  • Thanks! Will look into it. Could you please check out the new question posted by me - Footer Call Back getting called after AfterStep call... – Vicky Sep 13 '12 at 10:46
2

FlatFileItemWriter gives you option to add a header callback

headerCallback will be called before writing the first item to file.

So you need to Implement your Header Callback by implementing FlatFileHeaderCallback

Or Use Default Implementation

Mohsin Mansoor
  • 155
  • 1
  • 1
  • 8