CSVFormat.
The parser works record-wise. It is not possible to go back, once a record has been parsed from the input stream.
There are several static factory methods that can be used to create instances for various types of resources:
parse(java.io.File, Charset, CSVFormat) parse(String, CSVFormat)parse(java.net.URL, java.nio.charset.Charset, CSVFormat)
Alternatively parsers can also be created by passing a Reader directly to the sole constructor.
For those who like fluent APIs, parsers can be created using CSVFormat#parse(java. as a shortcut:
for (CSVRecord record : CSVFormat.EXCEL.parse(in)) {
...
}
To parse a CSV input from a file, you write:
File csvData = new File("/path/to/csv");
CSVParser parser = CSVParser.parse(csvData, CSVFormat.RFC4180);
for (CSVRecord csvRecord : parser) {
...
}
This will read the parse the contents of the file using the RFC 4180 format.
To parse CSV input in a format like Excel, you write:
CSVParser parser = CSVParser.parse(csvData, CSVFormat.EXCEL);
for (CSVRecord csvRecord : parser) {
...
}
If the predefined formats don't match the format at hand, custom formats can be defined. More information about
customizing CSVFormats is available in CSVFormat Javadoc.
If parsing record-wise is not desired, the contents of the input can be read completely into memory.
Reader in = new StringReader("a;b\nc;d");
CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
List<CSVRecord> list = parser.getRecords();
There are two constraints that have to be kept in mind:
The internal parser state is completely covered by the format and the reader state.
| Modifier and Type | Class and Description |
|---|---|
| public static class | CSVParser.
Builds a new |
| pack-priv class | |
| private static class | CSVParser.
Header information based on name and position. |
| Modifier and Type | Field and Description |
|---|---|
| private final long | characterOffset
Lexer offset when the parser does not start parsing at the beginning of the source. |
| private final CSVParser. | |
| private final CSVFormat | |
| private String | |
| private final CSVParser. | |
| private final Lexer | |
| private final List | recordList
A record buffer for getRecord(). |
| private long | recordNumber
The next record number to assign. |
| private final Token | |
| private String |
| Access | Constructor and Description |
|---|---|
| public | CSVParser(final Reader
a Reader containing CSV-formatted input. Must not be null. reader, final CSVFormat the CSVFormat used for CSV parsing. Must not be null. format)
Deprecated
Will be removed in the next major version, use
Builder#get().
Constructs a new instance using the given |
| public | CSVParser(final Reader
a Reader containing CSV-formatted input. Must not be null. reader, final CSVFormat the CSVFormat used for CSV parsing. Must not be null. format, final long Lexer offset when the parser does not start parsing at the beginning of the source. characterOffset, final long The next record number to assign. recordNumber)
Deprecated
Will be removed in the next major version, use
Builder#get().
Constructs a new instance using the given |
| private | CSVParser(final Reader
a Reader containing CSV-formatted input. Must not be null. reader, final CSVFormat the CSVFormat used for CSV parsing. Must not be null. format, final long Lexer offset when the parser does not start parsing at the beginning of the source. characterOffset, final long The next record number to assign. recordNumber, final Charset The character encoding to be used for the reader when enableByteTracking is true. charset, final boolean true to enable byte tracking for the parser; false to disable it.Constructs a new instance using the given |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| public static CSVParser. | |
| public void | |
| private Map | |
| private CSVParser. | Returns: null if the format has no header.Creates the name to index mapping if the format defines a header. |
| public long | Returns: current line number.Gets the current line number in the input stream. |
| public String | Returns: the first end-of-line string.Gets the first end-of-line string encountered. |
| public String | Returns: the header comment for this stream, or null if no comment is available.Gets the header comment, if any. |
| public Map | Returns: a copy of the header map.Gets a copy of the header map as defined in the CSVFormat's header. |
| pack-priv Map | |
| public List | Returns: read-only list of header names that iterates in column order.Gets a read-only list of header names that iterates in column order as defined in the CSVFormat's header. |
| public long | |
| public List | Returns: list ofCSVRecords, may be emptyParses the CSV input according to the given format and returns the content as a list of
|
| public String | Returns: the trailer comment for this stream, or null if no comment is available.Gets the trailer comment, if any. |
| private String | Returns: null if input is parsed as null, or input itself if the input isn't parsed as nullthe cell data to further processed input)Handles whether the input is parsed as null |
| public boolean | Returns: true if this parser has seen a header comment, false otherwiseChecks whether there is a header comment. |
| public boolean | Returns: true if this parser has seen a trailer comment, false otherwiseChecks whether there is a trailer comment. |
| public boolean | |
| private boolean | isStrictQuoteMode()
Tests whether the format's |
| public Iterator | |
| pack-priv CSVRecord | Returns: the record as an array of values, ornull if the end of the stream has been reached.Parses the next record from the current point in the stream. |
| public static CSVParser | Returns: a new parsera CSV file. Must not be null. file, final Charset The Charset to decode the given file, charset, final CSVFormat null maps to the default Charset.the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates a parser for the given |
| public static CSVParser | Returns: a new CSVParser configured with the given reader and format.an InputStream containing CSV-formatted input, inputStream, final Charset null maps to CSVFormat#DEFAULT.The Charset to decode the given file, charset, final CSVFormat null maps to the default Charset.the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates a CSV parser using the given |
| public static CSVParser | Returns: a new parsera CSV file. Must not be null. path, final Charset The Charset to decode the given file, charset, final CSVFormat null maps to the default Charset.the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates and returns a parser for the given |
| public static CSVParser | Returns: a new CSVParser configured with the given reader and format.a Reader containing CSV-formatted input. Must not be null. reader, final CSVFormat the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates a CSV parser using the given
If you do not read all records from the given |
| public static CSVParser | Returns: a new parsera CSV string. Must not be null. string, final CSVFormat the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates a parser for the given |
| public static CSVParser | Returns: a new parsera URL. Must not be null. url, final Charset the charset for the resource, charset, final CSVFormat null maps to the default Charset.the CSVFormat used for CSV parsing, format)null maps to CSVFormat#DEFAULT.Creates and returns a parser for the given URL, which the caller MUST close. |
| public Stream | Returns: a sequentialStream with this collection as its source.Returns a sequential |
| characterOffset | back to summary |
|---|---|
| private final long characterOffset Lexer offset when the parser does not start parsing at the beginning of the source. Usually used in combination
with | |
| csvRecordIterator | back to summary |
|---|---|
| private final CSVParser. | |
| format | back to summary |
|---|---|
| private final CSVFormat format | |
| headerComment | back to summary |
|---|---|
| private String headerComment | |
| headers | back to summary |
|---|---|
| private final CSVParser. | |
| lexer | back to summary |
|---|---|
| private final Lexer lexer | |
| recordList | back to summary |
|---|---|
| private final List<String> recordList A record buffer for getRecord(). Grows as necessary and is reused. | |
| recordNumber | back to summary |
|---|---|
| private long recordNumber The next record number to assign. | |
| reusableToken | back to summary |
|---|---|
| private final Token reusableToken | |
| trailerComment | back to summary |
|---|---|
| private String trailerComment | |
| CSVParser | back to summary |
|---|---|
| public CSVParser(final Reader reader, final CSVFormat format) throws IOException
Deprecated Will be removed in the next major version, use Constructs a new instance using the given
If you do not read all records from the given
| |
| CSVParser | back to summary |
|---|---|
| public CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber) throws IOException
Deprecated Will be removed in the next major version, use Constructs a new instance using the given
If you do not read all records from the given
| |
| CSVParser | back to summary |
|---|---|
| private CSVParser(final Reader reader, final CSVFormat format, final long characterOffset, final long recordNumber, final Charset charset, final boolean trackBytes) throws IOException Constructs a new instance using the given
If you do not read all records from the given
| |
| addRecordValue | back to summary |
|---|---|
| private void addRecordValue(final boolean lastRecord) | |
| builder | back to summary |
|---|---|
| public static CSVParser. Creates a new builder. | |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Closes resources.
| |
| createEmptyHeaderMap | back to summary |
|---|---|
| private Map | |
| createHeaders | back to summary |
|---|---|
| private CSVParser. Creates the name to index mapping if the format defines a header.
| |
| getCurrentLineNumber | back to summary |
|---|---|
| public long getCurrentLineNumber() Gets the current line number in the input stream. Note If your CSV input has multi-line values, the returned number does not correspond to the record number.
| |
| getFirstEndOfLine | back to summary |
|---|---|
| public String getFirstEndOfLine() Gets the first end-of-line string encountered.
| |
| getHeaderComment | back to summary |
|---|---|
| public String getHeaderComment() Gets the header comment, if any. The header comment appears before the header record.
| |
| getHeaderMap | back to summary |
|---|---|
| public Map Gets a copy of the header map as defined in the CSVFormat's header. The map keys are column names. The map values are 0-based indices. Note The map can only provide a one-to-one mapping when the format did not contain null or duplicate column names. | |
| getHeaderMapRaw | back to summary |
|---|---|
| pack-priv Map Gets the underlying header map. | |
| getHeaderNames | back to summary |
|---|---|
| public List Gets a read-only list of header names that iterates in column order as defined in the CSVFormat's header. Note The list provides strings that can be used as keys in the header map. The list will not contain null column names if they were present in the input format.
| |
| getRecordNumber | back to summary |
|---|---|
| public long getRecordNumber() Gets the current record number in the input stream. Note If your CSV input has multi-line values, the returned number does not correspond to the line number.
| |
| getRecords | back to summary |
|---|---|
| public List Parses the CSV input according to the given format and returns the content as a list of
The returned content starts at the current parse-position in the stream.
You can use
| |
| getTrailerComment | back to summary |
|---|---|
| public String getTrailerComment() Gets the trailer comment, if any. Trailer comments are located between the last record and EOF
| |
| handleNull | back to summary |
|---|---|
| private String handleNull(final String input) Handles whether the input is parsed as null | |
| hasHeaderComment | back to summary |
|---|---|
| public boolean hasHeaderComment() Checks whether there is a header comment.
The header comment appears before the header record.
Note that if the parser's format has been given an explicit header
(with
| |
| hasTrailerComment | back to summary |
|---|---|
| public boolean hasTrailerComment() Checks whether there is a trailer comment. Trailer comments are located between the last record and EOF. The trailer comments will only be available after the parser has finished processing this stream.
| |
| isClosed | back to summary |
|---|---|
| public boolean isClosed() Tests whether this parser is closed.
| |
| isStrictQuoteMode | back to summary |
|---|---|
| private boolean isStrictQuoteMode() Tests whether the format's
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements java. Returns the record iterator.
An
If the parser is closed, the iterator will not yield any more records.
A call to
If it is necessary to construct an iterator which is usable after the
parser is closed, one option is to extract all records as a list with
You can use
| |
| nextRecord | back to summary |
|---|---|
| pack-priv CSVRecord nextRecord() throws IOException Parses the next record from the current point in the stream.
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final File file, final Charset charset, final CSVFormat format) throws IOException Creates a parser for the given
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final InputStream inputStream, final Charset charset, final CSVFormat format) throws IOException Creates a CSV parser using the given
If you do not read all records from the given
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final Path path, final Charset charset, final CSVFormat format) throws IOException Creates and returns a parser for the given
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final Reader reader, final CSVFormat format) throws IOException Creates a CSV parser using the given
If you do not read all records from the given
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final String string, final CSVFormat format) throws IOException Creates a parser for the given
| |
| parse | back to summary |
|---|---|
| public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException Creates and returns a parser for the given URL, which the caller MUST close.
If you do not read all records from the given
| |
| stream | back to summary |
|---|---|
| public Stream Returns a sequential
If the parser is closed, the stream will not produce any more values.
See the comments in
You can use | |
CSVParser.
| Modifier and Type | Field and Description |
|---|---|
| private long | |
| private CSVFormat | |
| private long | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| protected |
| Modifier and Type | Method and Description |
|---|---|
| public CSVParser | |
| public CSVParser. | Returns: this instance.the lexer offset. characterOffset)Sets the lexer offset when the parser does not start parsing at the beginning of the source. |
| public CSVParser. | Returns: this instance.the CSV format, format)null resets to CSVFormat#DEFAULT.Sets the CSV format. |
| public CSVParser. | Returns: this instance.the next record number to assign. recordNumber)Sets the next record number to assign, defaults to |
| public CSVParser. | Returns: this instance.true to enable byte tracking; false to disable it.Sets whether to enable byte tracking for the parser. |
| characterOffset | back to summary |
|---|---|
| private long characterOffset | |
| format | back to summary |
|---|---|
| private CSVFormat format | |
| recordNumber | back to summary |
|---|---|
| private long recordNumber | |
| trackBytes | back to summary |
|---|---|
| private boolean trackBytes | |
| Builder | back to summary |
|---|---|
| protected Builder() Constructs a new instance. | |
| get | back to summary |
|---|---|
| public CSVParser get() throws IOException Implements org. Doc from org. Gets a result.
| |
| setCharacterOffset | back to summary |
|---|---|
| public CSVParser. Sets the lexer offset when the parser does not start parsing at the beginning of the source. | |
| setFormat | back to summary |
|---|---|
| public CSVParser. Sets the CSV format. A copy of the given format is kept.
| |
| setRecordNumber | back to summary |
|---|---|
| public CSVParser. Sets the next record number to assign, defaults to | |
| setTrackBytes | back to summary |
|---|---|
| public CSVParser. Sets whether to enable byte tracking for the parser. | |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private CSVRecord | Returns: the next record, ornull if the end of the stream has been reached.Gets the next record or null at the end of stream or max rows read. |
| public boolean | |
| public CSVRecord | |
| public void | remove()
Overrides default java. Removes from the underlying collection the last element returned by this iterator (optional operation). |
| current | back to summary |
|---|---|
| private CSVRecord current | |
| CSVRecordIterator | back to summary |
|---|---|
| pack-priv CSVRecordIterator() | |
| getNextRecord | back to summary |
|---|---|
| private CSVRecord getNextRecord() Gets the next record or null at the end of stream or max rows read.
| |
| hasNext | back to summary |
|---|---|
| public boolean hasNext() Implements java. Doc from java. Returns
| |
| next | back to summary |
|---|---|
| public CSVRecord next() Implements java. Doc from java. Returns the next element in the iteration. | |
| remove | back to summary |
|---|---|
| public void remove() Overrides default java. Doc from java. Removes from the underlying collection the last element returned
by this iterator (optional operation). This method can be called
only once per call to The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy.
The behavior of an iterator is unspecified if this method is called
after a call to the
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final Map | headerMap
Header column positions (0-based) |
| pack-priv final List | headerNames
Header names in column order |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| headerMap | back to summary |
|---|---|
| pack-priv final Map<String, Integer> headerMap Header column positions (0-based) | |
| headerNames | back to summary |
|---|---|
| pack-priv final List<String> headerNames Header names in column order | |