Spring Batch Flat File Item Writer Example

The following examples show how to use org.springframework.batch.item.file.FlatFileItemReader#setLinesToSkip.These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each exam. Spring Batch can use different data sources as input to batch processes. Data sources correspond to flat files, XML, and JavaScript Serialized Object Notation (JSON). Spring Batch also supports other types of data sources, such as Java Message Service (JMS), in the message-oriented middleware world. Spring-batch compositeWriter sample. GitHub Gist: instantly share code, notes, and snippets.

  1. Spring Batch Flat File Item Writer Example Free
  2. Spring Batch Flat File Item Writer Examples

In this article I explained how to use FlatFileItemWriter class to write a flat file without using a complete Spring batch flow.

Person.java domain object

PersonBuilder.java

This is a builder pattern for constructing a Person object:

Writer

HeaderCopyCallback.java

This is a class for writing header into flat file. For more details, see the file-writer.xml Spring bean context file:

FlatFileRecordsWriter.java

This is an interface for callback. See the implementation in FlatFileWriterTemplate.java, FileWriter.java class.

FlatFileWriterTemplate.java

Basically this template class is wrapping FlatFileItemWriter reference and also handling file opening,writing, and closing. This class is similar like JdbcTemplate

FlatFileWriter.java

FlatFileWriter class has two methods: write and writeAll. The first write method takes one argument, i.e. a Person object, and writes it to a flat file. Another writeAll method takes the list of Person objects as an argument and writes them into a flat file.

FlatFileWriterTest.java

The below JUnit class shows how to use to FlatFileWriter's write and writeAll methods.

file-writer.xml

flatFileWriter

This class is an item writer that writes data to a file or stream. The writer also provides a restart. The location of the output file is defined by a Resource and must represent a writable file. Uses buffered writers to improve performance.

The implementation is not thread-safe. (Spring doc)

DelimitedLineAggregator

A LineAggregator implementation that converts an object into a delimited list of strings. The default delimiter is a comma (from Spring doc).

BeanWrapperFieldExtractor

This is a field extractor for a Java bean. Given an array of property names, it will reflectively call getters on the item and return an array of all the values(from Spring docs). It extract Person's firstName,lastName, and middleName values and return to FlatFileItemWriter.

headerCopier

See the HeaderCopyCallback.java above for implementation. The bean provides header columns for the flat file.

Spring Batch Flat File Item Writer Example Free

outputResource

ClasspathResource-> Resource implementation for class path resources. Uses either a given ClassLoader or a given Class for loading resources (from Spring doc). This bean points to the flat file location after writing header and records.

writerManager

This bean has a FlatFileWriterTempate reference and the client is going to invoke a writerManager.write/writeAll method. See the above class FlatFileWriter.java and FlatFilwWriterTest.java for example.

Spring Batch Flat File Item Writer Examples

For the complete code please visit this link: https://github.com/upenderc/flatfile-hack