- All Implemented Interfaces:
Closeable
,AutoCloseable
,Readable
CSVWriter
and CSVReader
follows RFC 4180 but
provides options to aid in conversions between variants of this
format. For example, RFC 4180, as written, assumes that each field
contains printable 7-bit ASCII characters (excluding, for example,
control characters). CSVWriter
and CSVReader
will
allow any character to appear in a field. Options allow a choice
of line separators (the default is CRLF, the one used in RFC 4180).
The constructors' arguments indicate
- the reader providing the input. This reader should be configured to use the character set appropriate for the input.
- whether or not the first line of the input is a header.
- optionally a delimiter used as a line separator when a row is split between multiple lines. This delimiter is used to join adjacent lines to form a single row. If null the system's line separator is used. If carriage returns and line feeds should be reproduced "as is" when lines are joined to construct rows, One should specify a delimiter that matches the end-of-line convention used in the input.
For the simplest use of this class, one will first call a constructor.
If the constructor indicates that the first line is a header, the
method getHeaders()
will return an array containing
the headers in column order. Calling nextRow()
will
similarly return the fields of the next row in the input, finally
returning null when the end of the input is reached. For example,
One may also use theReader in = new FileReader("input.csv"); CSVReader r = new CSVReader(in, true); String[] headers = r.getHeaders(); String[] fields; while ((fields = r.nextRow()) != null) { ... } r.close();
Reader
read
methods
to read a field, followed by a call to nextField()
to go to the next field.
To specify a character encoding, the Reader
passed
as the first argument of a constructor should be an instance of
InputStreamReader
or a Reader
that
contains an instance of InputStreamReader
, possibly
with several intermediate readers.
-
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionConstructor.CSVReader
(Reader in, boolean hasHeader, LineReader.Delimiter delimiter) Constructor specifying a delimiter. -
Method Summary
Methods inherited from class java.io.Reader
mark, markSupported, nullReader, read, read, read, reset, skip, transferTo
-
Constructor Details
-
CSVReader
Constructor. When the first row is classified as a header, it is skipped but can be retrieved by callinggetHeaders()
.- Parameters:
in
- the inputhasHeader
- true if the first line (or row) is a header; false otherwise- Throws:
IOException
- an IO Exception was thrown
-
CSVReader
Constructor specifying a delimiter. When the row is classified as a header, it is skipped but can be retrieved by callinggetHeaders()
. If a field (which must be a quoted one in this case) is split between two lines, the specified delimiter will be inserted when the lines are joined to create a single row.- Parameters:
in
- the inputhasHeader
- true if the first line (or row) is a header; false otherwisedelimiter
- the delimiter (LineReader.Delimiter.LF
for a new line,LineReader.Delimiter.CR
for a carriage return, orLineReader.Delimiter.CRLF
for a carriage return followed by a new line; null for the system-defined line separator ("\n" on Unix system, "\r\n" on Microsoft Windows, and "\r" on the original MacOS systems)- Throws:
IOException
- an IO Exception was thrown
-
-
Method Details
-
getDelimiter
Get the delimiter. The specified delimiter is used to join lines when a row is split into multiple lines.- Returns:
- the delimiter; null for the system-defined line separator ("\n" on Unix system, "\r\n" on Microsoft Windows, and "\r" on the original MacOS systems).
-
getHeaders
Get the headers.- Returns:
- the headers; null if the constructor indicates that the first row does not contain headers
-
nextRow
Get the fields that make up the next row.- Returns:
- an array containing the fields in column order
- Throws:
IOException
- an IO Exception was thrown
-
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classReader
- Throws:
IOException
-
ready
This method always returns true because
read(char[],int,int)
returns -1 when the end of a field is reached. To obtain more characters to read,nextField()
should be called. That method may block until the next row is read.- Overrides:
ready
in classReader
- Throws:
IOException
-
read
This method returns -1 when the end of a field is reached. To read more data, one should call
nextField()
, which may block when the next row has to be read.- Specified by:
read
in classReader
- Throws:
IOException
-
nextField
Get the next field.- Returns:
- true if the next field has been read; false if there are no more fields to read
- Throws:
IOException
- an IO Exception was thrown
-