org.springframework.batch.item.file
Class FlatFileItemReader

java.lang.Object
  extended by org.springframework.batch.item.support.AbstractBufferedItemReaderItemStream
      extended by org.springframework.batch.item.file.FlatFileItemReader
All Implemented Interfaces:
ResourceAwareItemReaderItemStream, ItemReader, ItemStream, InitializingBean

public class FlatFileItemReader
extends AbstractBufferedItemReaderItemStream
implements ResourceAwareItemReaderItemStream, InitializingBean

This class represents a ItemReader, that reads lines from text file, tokenizes them to structured tuples (FieldSets) instances and maps the FieldSets to domain objects. The location of the file is defined by the resource property. To separate the structure of the file, LineTokenizer is used to parse data obtained from the file.
A FlatFileItemReader is not thread safe because it maintains state in the form of a ResourceLineReader. Be careful to configure a FlatFileItemReader using an appropriate factory or scope so that it is not shared between threads.

This class supports restart, skipping invalid lines and storing statistics. It can be configured to setup FieldSet column names from the file header, skip given number of lines at the beginning of the file.

The implementation is *not* thread-safe.

Author:
Waseem Malik, Tomas Slanina, Robert Kasanicky, Dave Syer

Field Summary
static String DEFAULT_CHARSET
           
 
Constructor Summary
FlatFileItemReader()
           
 
Method Summary
 void afterPropertiesSet()
           
protected  void doClose()
          Close the resources opened in AbstractBufferedItemReaderItemStream.doOpen().
protected  void doOpen()
          Open resources necessary to start reading input.
protected  Object doRead()
          Reads a line from input, tokenizes is it using the setLineTokenizer(LineTokenizer) and maps to domain object using setFieldSetMapper(FieldSetMapper).
protected  LineReader getReader()
           
 void setComments(String[] comments)
          Setter for comment prefixes.
 void setEncoding(String encoding)
          Setter for the encoding for this input source.
 void setFieldSetMapper(FieldSetMapper fieldSetMapper)
          Set the FieldSetMapper to be used for each line.
 void setFirstLineIsHeader(boolean firstLineIsHeader)
          Indicates whether first line is a header.
 void setLinesToSkip(int linesToSkip)
          Public setter for the number of lines to skip at the start of a file.
 void setLineTokenizer(LineTokenizer lineTokenizer)
           
 void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
          Public setter for the recordSeparatorPolicy.
 void setResource(Resource resource)
          Setter for resource property.
 
Methods inherited from class org.springframework.batch.item.support.AbstractBufferedItemReaderItemStream
close, getCurrentItemCount, jumpToItem, mark, open, read, reset, setCurrentItemCount, setName, setSaveState, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.batch.item.ItemReader
mark, read, reset
 
Methods inherited from interface org.springframework.batch.item.ItemStream
close, open, update
 

Field Detail

DEFAULT_CHARSET

public static final String DEFAULT_CHARSET
See Also:
Constant Field Values
Constructor Detail

FlatFileItemReader

public FlatFileItemReader()
Method Detail

getReader

protected LineReader getReader()
Returns:
line reader used to read input file

setResource

public void setResource(Resource resource)
Setter for resource property. The location of an input stream that can be read.

Specified by:
setResource in interface ResourceAwareItemReaderItemStream
Parameters:
resource -

setRecordSeparatorPolicy

public void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
Public setter for the recordSeparatorPolicy. Used to determine where the line endings are and do things like continue over a line ending if inside a quoted string.

Parameters:
recordSeparatorPolicy - the recordSeparatorPolicy to set

setComments

public void setComments(String[] comments)
Setter for comment prefixes. Can be used to ignore header lines as well by using e.g. the first couple of column names as a prefix.

Parameters:
comments - an array of comment line prefixes.

setFirstLineIsHeader

public void setFirstLineIsHeader(boolean firstLineIsHeader)
Indicates whether first line is a header. If the tokenizer is an AbstractLineTokenizer and the column names haven't been set already then the header will be used to setup column names. Default is false.


setLineTokenizer

public void setLineTokenizer(LineTokenizer lineTokenizer)
Parameters:
lineTokenizer - tokenizes each line from file into FieldSet.

setFieldSetMapper

public void setFieldSetMapper(FieldSetMapper fieldSetMapper)
Set the FieldSetMapper to be used for each line.

Parameters:
fieldSetMapper -

setLinesToSkip

public void setLinesToSkip(int linesToSkip)
Public setter for the number of lines to skip at the start of a file. Can be used if the file contains a header without useful (column name) information, and without a comment delimiter at the beginning of the lines.

Parameters:
linesToSkip - the number of lines to skip

setEncoding

public void setEncoding(String encoding)
Setter for the encoding for this input source. Default value is DEFAULT_CHARSET.

Parameters:
encoding - a properties object which possibly contains the encoding for this input file;

afterPropertiesSet

public void afterPropertiesSet()
                        throws Exception
Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception

doClose

protected void doClose()
                throws Exception
Description copied from class: AbstractBufferedItemReaderItemStream
Close the resources opened in AbstractBufferedItemReaderItemStream.doOpen().

Specified by:
doClose in class AbstractBufferedItemReaderItemStream
Throws:
Exception

doOpen

protected void doOpen()
               throws Exception
Description copied from class: AbstractBufferedItemReaderItemStream
Open resources necessary to start reading input.

Specified by:
doOpen in class AbstractBufferedItemReaderItemStream
Throws:
Exception

doRead

protected Object doRead()
                 throws Exception
Reads a line from input, tokenizes is it using the setLineTokenizer(LineTokenizer) and maps to domain object using setFieldSetMapper(FieldSetMapper).

Specified by:
doRead in class AbstractBufferedItemReaderItemStream
Returns:
item
Throws:
Exception
See Also:
ItemReader.read()


Copyright © 2009 SpringSource. All Rights Reserved.