public class CSVParser extends java.lang.Object implements java.lang.Iterable<CSVRecord>
CSVFormat
.
Parsing of a csv-string having tabs as separators, '"' as an optional value encapsulator, and comments starting with '#':
CSVFormat format = new CSVFormat('\t', '"', '#'); Reader in = new StringReader("a\tb\nc\td"); List<CSVRecord> records = new CSVParser(in, format).getRecords();
Parsing of a csv-string in Excel CSV format, using a for-each loop:
Reader in = new StringReader("a;b\nc;d"); CSVParser parser = new CSVParser(in, CSVFormat.EXCEL); for (CSVRecord record : parser) { ... }
Internal parser state is completely covered by the format and the reader-state.
see package documentation for more details
Constructor and Description |
---|
CSVParser(java.io.Reader input)
CSV parser using the default
CSVFormat . |
CSVParser(java.io.Reader input,
CSVFormat format)
Customized CSV parser using the given
CSVFormat |
CSVParser(java.lang.String input,
CSVFormat format)
Customized CSV parser using the given
CSVFormat |
Modifier and Type | Method and Description |
---|---|
java.util.Map<java.lang.String,java.lang.Integer> |
getHeaderMap()
Returns a copy of the header map that iterates in column order.
|
long |
getLineNumber()
Returns the current line number in the input stream.
|
long |
getRecordNumber()
Returns the current record number in the input stream.
|
java.util.List<CSVRecord> |
getRecords()
Parses the CSV input according to the given format and returns the content as an array of
CSVRecord
entries. |
java.util.Iterator<CSVRecord> |
iterator()
Returns an iterator on the records.
|
public CSVParser(java.io.Reader input) throws java.io.IOException
CSVFormat
.input
- a Reader containing "csv-formatted" inputjava.lang.IllegalArgumentException
- thrown if the parameters of the format are inconsistentjava.io.IOException
public CSVParser(java.io.Reader input, CSVFormat format) throws java.io.IOException
CSVFormat
input
- a Reader containing "csv-formatted" inputformat
- the CSVFormat used for CSV parsingjava.lang.IllegalArgumentException
- thrown if the parameters of the format are inconsistentjava.io.IOException
public CSVParser(java.lang.String input, CSVFormat format) throws java.io.IOException
CSVFormat
input
- a String containing "csv-formatted" inputformat
- the CSVFormat used for CSV parsingjava.lang.IllegalArgumentException
- thrown if the parameters of the format are inconsistentjava.io.IOException
public java.util.Map<java.lang.String,java.lang.Integer> getHeaderMap()
The map keys are column names. The map values are 0-based indices.
public long getLineNumber()
public long getRecordNumber()
public java.util.List<CSVRecord> getRecords() throws java.io.IOException
CSVRecord
entries.
The returned content starts at the current parse-position in the stream.CSVRecord
entries, may be emptyjava.io.IOException
- on parse error or input read-failureThis software is published under the BSD license. Copyright © 2003-${build.year}, CRIXP AG, Switzerland, All rights reserved. Use is subject to license terms.