Package org.beanio.stream.csv
Class CsvReader
- java.lang.Object
-
- org.beanio.stream.csv.CsvReader
-
- All Implemented Interfaces:
RecordReader
public class CsvReader extends java.lang.Object implements RecordReader
ACsvReader
is used to parse CSV formatted flat files into records ofString
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
totrue
when constructing the reader.The reader also supports the following customizations:
- The default quotation mark character, '"', can be overridden.
- The default escape character, '"', can be overridden or disabled altogether.
- Whitespace can be allowed outside of quoted values.
- Quotation marks can be allowed in unquoted fields (as long as the quotation mark is not the first character in the field
The reader will not recognize an escape character used outside of a quoted field.
- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor Description CsvReader(java.io.Reader in)
Constructs a newCsvReader
.CsvReader(java.io.Reader in, CsvParserConfiguration config)
Constructs a newCsvReader
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Closes this input stream.int
getRecordLineNumber()
Returns the starting line number of the last record record.java.lang.String
getRecordText()
Returns the raw text of the last record read or null if the end of the stream was reached.java.lang.String[]
read()
Reads the next record from this input stream.
-
-
-
Constructor Detail
-
CsvReader
public CsvReader(java.io.Reader in)
Constructs a newCsvReader
.- Parameters:
in
- the input stream to read from
-
CsvReader
public CsvReader(java.io.Reader in, CsvParserConfiguration config) throws java.lang.IllegalArgumentException
Constructs a newCsvReader
.- Parameters:
in
- the input stream to read fromconfig
- the reader configuration settings ornull
to accept defaults- Throws:
java.lang.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 interfaceRecordReader
- Returns:
- the starting line number of the last record
-
getRecordText
public java.lang.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 interfaceRecordReader
- Returns:
- the raw text of the last record
-
read
public java.lang.String[] read() throws java.io.IOException, RecordIOException
Reads the next record from this input stream.- Specified by:
read
in interfaceRecordReader
- Returns:
- the array of field values that make up the next record read from the stream
- Throws:
java.io.IOException
- if an I/O error occursRecordIOException
- if the record is malformed and cannot be parsed, but subsequent reads may still be possible
-
close
public void close() throws java.io.IOException
Description copied from interface:RecordReader
Closes this input stream.- Specified by:
close
in interfaceRecordReader
- Throws:
java.io.IOException
- if an I/O error occurs closing the stream
-
-