org.apache.commons.csv
Class CSVParser

java.lang.Object
  extended by org.apache.commons.csv.CSVParser
All Implemented Interfaces:
Iterable<CSVRecord>

public class CSVParser
extends Object
implements Iterable<CSVRecord>

Parses CSV files according to the specified configuration. Because CSV appears in many different dialects, the parser supports many configuration settings by allowing the specification of a CSVFormat.

To parse a CSV input with tabs as separators, '"' (double-quote) as an optional value encapsulator, and comments starting with '#', you write:

 Reader in = new StringReader("a\tb\nc\td");
 Iterable<CSVRecord> parser = CSVFormat.newBuilder()
     .withCommentStart('#')
     .withDelimiter('\t')
     .withQuoteChar('"').parse(in);
  for (CSVRecord csvRecord : parse) {
     ...
  }
 

To parse CSV input in a given format like Excel, you write:

 Reader in = new StringReader("a;b\nc;d");
 Iterable<CSVRecord> parser = CSVFormat.EXCEL.parse(in);
 for (CSVRecord record : parser) {
     ...
 }
 

You may also get a List of records:

 Reader in = new StringReader("a;b\nc;d");
 CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
 List<CSVRecord> list = parser.getRecords();
 

Internal parser state is completely covered by the format and the reader-state.

see package documentation for more details

Version:
$Id: CSVParser.java 1461307 2013-03-26 20:52:28Z ggregory $

Constructor Summary
CSVParser(Reader input)
          CSV parser using the default CSVFormat.
CSVParser(Reader input, CSVFormat format)
          Customized CSV parser using the given CSVFormat
CSVParser(String input, CSVFormat format)
          Customized CSV parser using the given CSVFormat
 
Method Summary
 Map<String,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.
 List<CSVRecord> getRecords()
          Parses the CSV input according to the given format and returns the content as an array of CSVRecord entries.
 Iterator<CSVRecord> iterator()
          Returns an iterator on the records.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVParser

public CSVParser(Reader input)
          throws IOException
CSV parser using the default CSVFormat.

Parameters:
input - a Reader containing "csv-formatted" input
Throws:
IllegalArgumentException - thrown if the parameters of the format are inconsistent
IOException - If an I/O error occurs

CSVParser

public CSVParser(Reader input,
                 CSVFormat format)
          throws IOException
Customized CSV parser using the given CSVFormat

Parameters:
input - a Reader containing CSV-formatted input
format - the CSVFormat used for CSV parsing
Throws:
IllegalArgumentException - thrown if the parameters of the format are inconsistent
IOException - If an I/O error occurs

CSVParser

public CSVParser(String input,
                 CSVFormat format)
          throws IOException
Customized CSV parser using the given CSVFormat

Parameters:
input - a String containing "csv-formatted" input
format - the CSVFormat used for CSV parsing
Throws:
IllegalArgumentException - thrown if the parameters of the format are inconsistent
IOException - If an I/O error occurs
Method Detail

getHeaderMap

public Map<String,Integer> getHeaderMap()
Returns a copy of the header map that iterates in column order.

The map keys are column names. The map values are 0-based indices.

Returns:
a copy of the header map that iterates in column order.

getLineNumber

public long getLineNumber()
Returns the current line number in the input stream.

ATTENTION: If your CSV input has multi-line values, the returned number does not correspond to the record number.

Returns:
current line number

getRecordNumber

public long getRecordNumber()
Returns the current record number in the input stream.

ATTENTION: If your CSV input has multi-line values, the returned number does not correspond to the line number.

Returns:
current line number

getRecords

public List<CSVRecord> getRecords()
                           throws IOException
Parses the CSV input according to the given format and returns the content as an array of CSVRecord entries.

The returned content starts at the current parse-position in the stream.

Returns:
list of CSVRecord entries, may be empty
Throws:
IOException - on parse error or input read-failure

iterator

public Iterator<CSVRecord> iterator()
Returns an iterator on the records. IOExceptions occurring during the iteration are wrapped in a RuntimeException.

Specified by:
iterator in interface Iterable<CSVRecord>


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.