BeanIO 1.2.3

org.beanio.stream.csv
Class CsvReader

java.lang.Object
  extended by org.beanio.stream.csv.CsvReader
All Implemented Interfaces:
RecordReader

public class CsvReader
extends Object
implements RecordReader

A CsvReader is used to parse CSV formatted flat files into records of String arrays. The CSV format supported is defined by specification RFC 4180. By default, there is one exception: lines that span multiple records will throw an exception. To allow quoted multi-line fields, simply set multilineEnabled to true when constructing the reader.

The reader also supports the following customizations:

The reader will not recognize an escape character used outside of a quoted field.

Since:
1.0
Author:
Kevin Seim

Constructor Summary
CsvReader(Reader in)
          Constructs a new CsvReader.
CsvReader(Reader in, char delimiter, Character escape, boolean multilineEnabled)
          Deprecated. use CsvReader(Reader, CsvReaderConfiguration) instead
CsvReader(Reader in, char delimiter, char quote, Character escape, boolean multilineEnabled, boolean whitespaceAllowed, boolean unquotedQuotesAllowed)
          Deprecated. use CsvReader(Reader, CsvReaderConfiguration) instead
CsvReader(Reader in, CsvReaderConfiguration config)
          Constructs a new CsvReader.
 
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 the next 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

CsvReader

public CsvReader(Reader in)
Constructs a new CsvReader.

Parameters:
in - the input stream to read from

CsvReader

public CsvReader(Reader in,
                 char delimiter,
                 Character escape,
                 boolean multilineEnabled)
Deprecated. use CsvReader(Reader, CsvReaderConfiguration) instead

Constructs a new CsvReader.

Parameters:
in - the input stream to read from
delimiter - the field delimiter
escape - the escape character, or null to disable escaping
multilineEnabled - set to true to allow quoted fields to contain newline and carriage return characters

CsvReader

public CsvReader(Reader in,
                 char delimiter,
                 char quote,
                 Character escape,
                 boolean multilineEnabled,
                 boolean whitespaceAllowed,
                 boolean unquotedQuotesAllowed)
          throws IllegalArgumentException
Deprecated. use CsvReader(Reader, CsvReaderConfiguration) instead

Constructs a new CsvReader.

Parameters:
in - the input stream to read from
delimiter - the field delimiter
quote - the quotation character
escape - the escape character, or null to disable escaping
multilineEnabled - set to true to allow quoted fields to contain newline and carriage return characters
whitespaceAllowed - set to true to ignore whitespace outside of quoted fields
unquotedQuotesAllowed - set to true to allow fields containing quotation marks in unquoted fields, for example: 'Mark said "...'. Note that if the quotation mark is at the beginning of the field, the field is considered quoted and a trailing quotation mark is required.
Throws:
IllegalArgumentException - if the configuration is invalid

CsvReader

public CsvReader(Reader in,
                 CsvReaderConfiguration config)
          throws IllegalArgumentException
Constructs a new CsvReader.

Parameters:
in - the input stream to read from
config - the reader configuration settings or null to accept defaults
Throws:
IllegalArgumentException - if a configuration setting is invalid
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.

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,
                     RecordIOException
Reads the next record from this input stream.

Specified by:
read in interface RecordReader
Returns:
the array of field values that make up the next record read from the stream
Throws:
IOException - if an I/O error occurs
RecordIOException - if the record is malformed and cannot be parsed, but subsequent reads may still be possible

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