|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.springframework.batch.item.ExecutionContextUserSupport
org.springframework.batch.item.file.FlatFileItemReader
public class FlatFileItemReader
This class represents a ItemReader
, that reads lines from text file,
tokenizes them to structured tuples (FieldSet
s) instances and maps
the FieldSet
s 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.
Field Summary | |
---|---|
static String |
DEFAULT_CHARSET
|
Constructor Summary | |
---|---|
FlatFileItemReader()
|
Method Summary | |
---|---|
void |
afterPropertiesSet()
|
void |
close(ExecutionContext executionContext)
Close and null out the reader. |
protected LineReader |
getReader()
|
void |
mark()
Mark is supported as long as this ItemStream is used in a
single-threaded environment. |
void |
open(ExecutionContext executionContext)
Initialize the reader if necessary. |
Object |
read()
Reads a line from input, tokenizes is it using the setLineTokenizer(LineTokenizer) and maps to domain object using
setFieldSetMapper(FieldSetMapper) . |
void |
reset()
Reset the stream to the last mark. |
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. |
void |
setSaveState(boolean saveState)
Set the boolean indicating whether or not state should be saved in the provided ExecutionContext during the ItemStream call to
update. |
void |
update(ExecutionContext executionContext)
This method returns the execution attributes for the reader. |
Methods inherited from class org.springframework.batch.item.ExecutionContextUserSupport |
---|
getKey, setName |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final String DEFAULT_CHARSET
Constructor Detail |
---|
public FlatFileItemReader()
Method Detail |
---|
public void open(ExecutionContext executionContext) throws ItemStreamException
open
in interface ItemStream
IllegalStateException
- if the resource cannot be opened
ItemStreamException
public void close(ExecutionContext executionContext) throws ItemStreamException
close
in interface ItemStream
executionContext
- TODO
ItemStreamException
public Object read() throws Exception
setLineTokenizer(LineTokenizer)
and maps to domain object using
setFieldSetMapper(FieldSetMapper)
.
read
in interface ItemReader
Exception
- if an underlying resource is unavailable.ItemReader.read()
public void update(ExecutionContext executionContext)
update
in interface ItemStream
executionContext
- to be updatedpublic void mark()
ItemStream
is used in a
single-threaded environment. The state backing the mark is a single
counter, keeping track of the current position, so multiple threads
cannot be accommodated.
mark
in interface ItemReader
ItemReader.mark()
public void reset()
ItemReader
reset
in interface ItemReader
protected LineReader getReader()
public void setResource(Resource resource)
resource
- public void setRecordSeparatorPolicy(RecordSeparatorPolicy recordSeparatorPolicy)
recordSeparatorPolicy
- the recordSeparatorPolicy to setpublic void setComments(String[] comments)
comments
- an array of comment line prefixes.public void setFirstLineIsHeader(boolean firstLineIsHeader)
AbstractLineTokenizer
and the column names haven't been set
already then the header will be used to setup column names. Default is
false
.
public void setLineTokenizer(LineTokenizer lineTokenizer)
lineTokenizer
- tokenizes each line from file into FieldSet
.public void setFieldSetMapper(FieldSetMapper fieldSetMapper)
fieldSetMapper
- public void setLinesToSkip(int linesToSkip)
linesToSkip
- the number of lines to skippublic void setEncoding(String encoding)
DEFAULT_CHARSET
.
encoding
- a properties object which possibly contains the encoding
for this input file;public void afterPropertiesSet() throws Exception
afterPropertiesSet
in interface InitializingBean
Exception
public void setSaveState(boolean saveState)
ExecutionContext
during the ItemStream
call to
update. Setting this to false means that it will always start at the
beginning.
saveState
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |