title: Spring-Batch Java Based FileItemWriter for CSV
body: <p>I have a Spring Batch service containg ItemWriter to write the data to the CSV.
  I have used the example give by Spring Batch guide. <a href="https://spring.io/guides/gs/batch-processing/" rel="nofollow">https://spring.io/guides/gs/batch-processing/</a></p>
  
  <p>I tried to modify the ItemWriter to create the CSV again.</p>
  
  <p>The Problems which I am facing are -</p>
  
  <ol>
  <li>It is not creating the CSV file if it is not present.</li>
  <li>If I made it available before hand it is not writing data to it.</li>
  </ol>
  
  <hr>
  
  <pre><code>@Bean
  public ItemWriter&lt;Person&gt; writer(DataSource dataSource) {
  
  FlatFileitemWriter&lt;Person&gt; csvWriter = new FlatFileItemWriter&lt;Person&gt;();
  csvWriter.setResource(new ClassPathResource("csv/new-data.csv"));
  csvWriter.setShouldDeleteIfExists(true);
  DelimitedLineAggregator&lt;Person&gt; lineAggregator = new DelimitedLineAggregator&lt;Person&gt;();
  lineAggregator.setDelimiter(","); 
  
  BeanWrapperFieldExtractor&lt;Person&gt; fieldExtractor = new BeanWrapperFieldExtractor&lt;Person&gt;();
  String[] names = {"firstName", "lastName"};
  fieldExtractor.setNames(names);
  lineAggregator.setFieldExtractor(fieldExtractor);
  csvWriter.setLineAggregator(lineAggregator);
      return csvWriter;
  }
  </code></pre>
  
  <p>I have gone through various links but they show the example with XML based configuration. How to do it completely in JAVA ? </p>