BeanIO 1.2.3

org.beanio.stream.delimited
Class DelimitedReader

java.lang.Object
  extended by org.beanio.stream.delimited.DelimitedReader
All Implemented Interfaces:
RecordReader

public class DelimitedReader
extends Object
implements RecordReader

A DelimitedReader is used to parse delimited flat files into records of String arrays. Records must be terminated by a single configurable character, or by default, any of the following: line feed (LF), carriage return (CR), or CRLF combination. And fields that make up a record must be delimited by a single configurable character.

If an escape character is configured, the delimiting character can be be escaped in a field by placing the escape character immediately before the delimiter. The escape character may also be used to escape itself. For example, using a comma delimiter and backslash escape:

 Field1,Field2\,Field3,Field\\4,Field\5
 
The record would be parsed as "Field1", "Field2,Field3", "Field\4", "Field\5"

Additionally, if a record may span multiple lines, a single line continuation character can be configured. The line continuation character must immediately precede the record termination character. For example, using a comma delimiter and backslash line continuation character:

 Field1,Field2\
 Field3
 
 The 2 lines would be parsed as a single record with values "Field1", "Field2", "Field3".
 

The same character can be used for line continuation and escaping, but neither can match the delimiter.

Since:
1.0
Author:
Kevin Seim

Constructor Summary
DelimitedReader(Reader in)
          Constructs a new DelimitedReader using a tab character for the field delimiter.
DelimitedReader(Reader in, char delimiter)
          Constructs a new DelimitedReader.
DelimitedReader(Reader in, char delimiter, Character escapeChar, Character lineContinuationCharacter)
          Deprecated. use DelimitedReader(Reader, DelimitedReaderConfiguration) instead
DelimitedReader(Reader in, char delimiter, Character escapeChar, Character lineContinuationCharacter, Character recordTerminator)
          Deprecated. use DelimitedReader(Reader, DelimitedReaderConfiguration) instead
DelimitedReader(Reader in, DelimitedReaderConfiguration config)
          Constructs a new DelimitedReader.
 
Method Summary
 void close()
          Closes this input stream.
 int getRecordLineNumber()
          Returns the starting line number of the last record record.
 String getRecordText()
          Returns the raw text of the last record read or null if the end of the stream was reached.
 String[] read()
          Reads a single record from this input stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelimitedReader

public DelimitedReader(Reader in)
Constructs a new DelimitedReader using a tab character for the field delimiter. Escaping is and line continuation characters are disabled.

Parameters:
in - the input stream to read from

DelimitedReader

public DelimitedReader(Reader in,
                       char delimiter)
Constructs a new DelimitedReader. Escaping and line continuation characters are disabled.

Parameters:
in - the input stream to read from
delimiter - the field delimiting character

DelimitedReader

public DelimitedReader(Reader in,
                       char delimiter,
                       Character escapeChar,
                       Character lineContinuationCharacter)
Deprecated. use DelimitedReader(Reader, DelimitedReaderConfiguration) instead

Constructs a new DelimitedReader.

Parameters:
in - the input stream to read from
delimiter - the field delimiting character
escapeChar - the escape character, or null to disable escaping
lineContinuationCharacter - the line continuation character, or null to disable line continuations
Throws:
IllegalArgumentException - if the delimiter matches the escape character or or the line continuation character

DelimitedReader

public DelimitedReader(Reader in,
                       char delimiter,
                       Character escapeChar,
                       Character lineContinuationCharacter,
                       Character recordTerminator)
Deprecated. use DelimitedReader(Reader, DelimitedReaderConfiguration) instead

Constructs a new DelimitedReader.

Parameters:
in - the input stream to read from
delimiter - the field delimiting character
escapeChar - the escape character, or null to disable escaping
lineContinuationCharacter - the line continuation character, or null to disable line continuations
recordTerminator - the character used to signify the end of the record, or null to allow any of LF, CR, or CRLF
Throws:
IllegalArgumentException - if the delimiter matches the escape character or or the line continuation character

DelimitedReader

public DelimitedReader(Reader in,
                       DelimitedReaderConfiguration config)
Constructs a new DelimitedReader.

Parameters:
in - the input stream to read from
config - the reader configuration settings or null to use default values
Throws:
IllegalArgumentException - if the delimiter matches the escape character or or the line continuation character
Since:
1.2
Method Detail

getRecordLineNumber

public int getRecordLineNumber()
Returns the starting line number of the last record record. A value of -1 is returned if the end of the stream was reached, or 0 if records are not terminated by new line characters.

Specified by:
getRecordLineNumber in interface RecordReader
Returns:
the starting line number of the last record

getRecordText

public String getRecordText()
Returns the raw text of the last record read or null if the end of the stream was reached.

Specified by:
getRecordText in interface RecordReader
Returns:
the raw text of the last record

read

public String[] read()
              throws IOException
Description copied from interface: RecordReader
Reads a single record from this input stream. The type of object returned depends on the format of the stream.

Specified by:
read in interface RecordReader
Returns:
the record value, or null if the end of the stream was reached.
Throws:
IOException - if an I/O error occurs reading from the stream

close

public void close()
           throws IOException
Description copied from interface: RecordReader
Closes this input stream.

Specified by:
close in interface RecordReader
Throws:
IOException - if an I/O error occurs closing the stream

BeanIO 1.2.3

Copyright © 2010-2012 Kevin Seim