title: Spring batch: Writing column names as first line in flat file
body: <p>I want to create a flat file which has the below format:</p>
  
  <pre><code>Col1Name;Col2Name;Col3Name
  one;23;20120912
  two;28;20120712
  </code></pre>
  
  <p>As seen, the first line in the flat file are the column names. </p>
  
  <p>How to achieve this through header callback ?</p>
  
  <p>I see that if the input file is of above format, there is an option as below to ignore first line:</p>
  
  <pre><code>&lt;property name="firstLineIsHeader" value="true"/&gt;
  </code></pre>
  
  <p>Also, this <a href="https://jira.springsource.org/browse/BATCH-211" rel="nofollow noreferrer">Jira Issue</a> indicates that what I want is implemeted and closed. However, I am unable to find any example for writing first line as column names.</p>
  
  <pre><code>&lt;beans:bean id="MyFileItemWriter" class="com.nik.MyFileItemWriter" scope="step"&gt;
      &lt;beans:property name="delegate"&gt;
          &lt;beans:bean class="org.springframework.batch.item.file.FlatFileItemWriter"&gt;
              &lt;beans:property name="resource" value="file:MYFILE.dat" /&gt; 
  
              &lt;beans:property name="lineAggregator"&gt;
                  &lt;beans:bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator"&gt;
                      &lt;beans:property name="delimiter" value=";" /&gt;
                      &lt;beans:property name="fieldExtractor"&gt;
                          &lt;beans:bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor"&gt;
                              &lt;beans:property name="names" value="Col1Name, Col2Name, Col3Name" /&gt;
                          &lt;/beans:bean&gt;
                      &lt;/beans:property&gt;
                  &lt;/beans:bean&gt;
              &lt;/beans:property&gt;
              &lt;beans:property name="headerCallback" ref="MyFileItemWriter" /&gt;
          &lt;/beans:bean&gt;
      &lt;/beans:property&gt;
  &lt;/beans:bean&gt;
  </code></pre>
  
  <p>My Item Writer looks as below:</p>
  
  <pre><code>public class MyFileItemWriter implements ItemWriter&lt;MyBean&gt;, FlatFileHeaderCallback, ItemStream{
  
  private FlatFileItemWriter&lt;MyBean&gt; delegate;    
  
   public void setDelegate(final FlatFileItemWriter&lt;MyBean&gt; delegate) {
          this.delegate = delegate;
      }
  
  public void writeHeader(Writer writer) throws IOException {
  
  
  }
  
  public void write(List&lt;? extends MyBean&gt; 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);        
  }
  
  }
  </code></pre>
  
  <p>Thanks for reading!</p>