public class FlatFileItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements ResourceAwareItemReaderItemStream<T>, org.springframework.beans.factory.InitializingBean
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.Modifier and Type | Field and Description |
---|---|
protected java.lang.String[] |
comments |
static java.lang.String |
DEFAULT_CHARSET |
static java.lang.String[] |
DEFAULT_COMMENT_PREFIXES |
Constructor and Description |
---|
FlatFileItemReader() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected void |
doClose()
Close the resources opened in
AbstractItemCountingItemStreamItemReader.doOpen() . |
protected void |
doOpen()
Open resources necessary to start reading input.
|
protected T |
doRead()
Read next item from input.
|
protected boolean |
isComment(java.lang.String line) |
protected void |
jumpToItem(int itemIndex)
Move to the given item index.
|
void |
setBufferedReaderFactory(BufferedReaderFactory bufferedReaderFactory)
Factory for the
BufferedReader that will be used to extract lines from the file. |
void |
setComments(java.lang.String[] comments)
Setter for comment prefixes.
|
void |
setEncoding(java.lang.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 on
AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext) if the input resource does not exist. |
close, getCurrentItemCount, isSaveState, open, read, setCurrentItemCount, setMaxItemCount, setSaveState, update
getExecutionContextKey, setExecutionContextName, setName
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
close, open, update
read
public static final java.lang.String DEFAULT_CHARSET
public static final java.lang.String[] DEFAULT_COMMENT_PREFIXES
protected java.lang.String[] comments
public void setStrict(boolean strict)
AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)
if the input resource does not exist.strict
- true
by defaultpublic void setSkippedLinesCallback(LineCallbackHandler skippedLinesCallback)
skippedLinesCallback
- will be called for each one of the initial skipped lines before any items are read.public void setLinesToSkip(int linesToSkip)
linesToSkip
- the number of lines to skippublic void setLineMapper(LineMapper<T> lineMapper)
lineMapper
- maps line to itempublic void setEncoding(java.lang.String encoding)
DEFAULT_CHARSET
.encoding
- a properties object which possibly contains the encoding for this input file;public void setBufferedReaderFactory(BufferedReaderFactory bufferedReaderFactory)
BufferedReader
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.bufferedReaderFactory
- the bufferedReaderFactory to setpublic void setComments(java.lang.String[] comments)
DEFAULT_COMMENT_PREFIXES
.comments
- an array of comment line prefixes.public void setResource(org.springframework.core.io.Resource resource)
setResource
in interface ResourceAwareItemReaderItemStream<T>
public void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
recordSeparatorPolicy
- the recordSeparatorPolicy to set@Nullable protected T doRead() throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
doRead
in class AbstractItemCountingItemStreamItemReader<T>
setRecordSeparatorPolicy(RecordSeparatorPolicy)
(might span multiple lines in file).java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the frameworkprotected boolean isComment(java.lang.String line)
protected void doClose() throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
AbstractItemCountingItemStreamItemReader.doOpen()
.doClose
in class AbstractItemCountingItemStreamItemReader<T>
java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the frameworkprotected void doOpen() throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
doOpen
in class AbstractItemCountingItemStreamItemReader<T>
java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the frameworkpublic void afterPropertiesSet() throws java.lang.Exception
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
java.lang.Exception
protected void jumpToItem(int itemIndex) throws java.lang.Exception
AbstractItemCountingItemStreamItemReader
AbstractItemCountingItemStreamItemReader.doRead()
.jumpToItem
in class AbstractItemCountingItemStreamItemReader<T>
itemIndex
- index of item (0 based) to jump to.java.lang.Exception
- Allows subclasses to throw checked exceptions for interpretation by the framework