Class FlatFileItemReader<T>
java.lang.Object
org.springframework.batch.item.ItemStreamSupport
org.springframework.batch.item.support.AbstractItemStreamItemReader<T>
org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader<T>
org.springframework.batch.item.file.FlatFileItemReader<T>
- All Implemented Interfaces:
ResourceAwareItemReaderItemStream<T>
,ItemReader<T>
,ItemStream
,ItemStreamReader<T>
,org.springframework.beans.factory.InitializingBean
public class FlatFileItemReader<T>
extends AbstractItemCountingItemStreamItemReader<T>
implements ResourceAwareItemReaderItemStream<T>, org.springframework.beans.factory.InitializingBean
Restartable
ItemReader
that reads lines from input
setResource(Resource)
. Line is defined by the
setRecordSeparatorPolicy(RecordSeparatorPolicy)
and mapped to item using
setLineMapper(LineMapper)
. If an exception is thrown during line mapping it is
rethrown as FlatFileParseException
adding information about the problematic
line and its line number.- Author:
- Robert Kasanicky, Mahmoud Ben Hassine
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
protected void
doClose()
Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen()
.protected void
doOpen()
Open resources necessary to start reading input.protected T
doRead()
Read next item from input.protected boolean
protected void
jumpToItem
(int itemIndex) Move to the given item index.void
setBufferedReaderFactory
(BufferedReaderFactory bufferedReaderFactory) Factory for theBufferedReader
that will be used to extract lines from the file.void
setComments
(String[] comments) Setter for comment prefixes.void
setEncoding
(String encoding) Setter for the encoding for this input source.void
setLineMapper
(LineMapper<T> lineMapper) Setter for line mapper.void
setLinesToSkip
(int linesToSkip) Public setter for the number of lines to skip at the start of a file.void
setRecordSeparatorPolicy
(RecordSeparatorPolicy recordSeparatorPolicy) Public setter for the recordSeparatorPolicy.void
setResource
(org.springframework.core.io.Resource resource) Public setter for the input resource.void
setSkippedLinesCallback
(LineCallbackHandler skippedLinesCallback) void
setStrict
(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)
if the input resource does not exist.Methods inherited from class org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
Methods inherited from class org.springframework.batch.item.ItemStreamSupport
getExecutionContextKey, setExecutionContextName, setName
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
read
Methods inherited from interface org.springframework.batch.item.ItemStream
close, open, update
-
Field Details
-
DEFAULT_CHARSET
-
DEFAULT_COMMENT_PREFIXES
-
comments
-
-
Constructor Details
-
FlatFileItemReader
public FlatFileItemReader()
-
-
Method Details
-
setStrict
public void setStrict(boolean strict) In strict mode the reader will throw an exception onAbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)
if the input resource does not exist.- Parameters:
strict
-true
by default
-
setSkippedLinesCallback
- Parameters:
skippedLinesCallback
- will be called for each one of the initial skipped lines before any items are read.
-
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
-
setLineMapper
Setter for line mapper. This property is required to be set.- Parameters:
lineMapper
- maps line to item
-
setEncoding
Setter for the encoding for this input source. Default value isDEFAULT_CHARSET
.- Parameters:
encoding
- a properties object which possibly contains the encoding for this input file;
-
setBufferedReaderFactory
Factory for theBufferedReader
that will be used to extract lines from the file. The default is fine for plain text files, but this is a useful strategy for binary files where the standard BufferedReader from java.io is limiting.- Parameters:
bufferedReaderFactory
- the bufferedReaderFactory to set
-
setComments
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. Defaults toDEFAULT_COMMENT_PREFIXES
.- Parameters:
comments
- an array of comment line prefixes.
-
setResource
public void setResource(org.springframework.core.io.Resource resource) Public setter for the input resource.- Specified by:
setResource
in interfaceResourceAwareItemReaderItemStream<T>
-
setRecordSeparatorPolicy
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
-
doRead
Description copied from class:AbstractItemCountingItemStreamItemReader
Read next item from input.- Specified by:
doRead
in classAbstractItemCountingItemStreamItemReader<T>
- Returns:
- string corresponding to logical record according to
setRecordSeparatorPolicy(RecordSeparatorPolicy)
(might span multiple lines in file). - Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
isComment
-
doClose
Description copied from class:AbstractItemCountingItemStreamItemReader
Close the resources opened inAbstractItemCountingItemStreamItemReader.doOpen()
.- Specified by:
doClose
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
doOpen
Description copied from class:AbstractItemCountingItemStreamItemReader
Open resources necessary to start reading input.- Specified by:
doOpen
in classAbstractItemCountingItemStreamItemReader<T>
- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-
afterPropertiesSet
- Specified by:
afterPropertiesSet
in interfaceorg.springframework.beans.factory.InitializingBean
- Throws:
Exception
-
jumpToItem
Description copied from class:AbstractItemCountingItemStreamItemReader
Move to the given item index. Subclasses should override this method if there is a more efficient way of moving to given index than re-reading the input usingAbstractItemCountingItemStreamItemReader.doRead()
.- Overrides:
jumpToItem
in classAbstractItemCountingItemStreamItemReader<T>
- Parameters:
itemIndex
- index of item (0 based) to jump to.- Throws:
Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework
-