com.devexperts.io
Class CSVReader

java.lang.Object
  extended by com.devexperts.io.CSVReader
All Implemented Interfaces:
Closeable

public class CSVReader
extends Object
implements Closeable

Reads data from the stream using Comma-Separated Values (CSV) format. See RFC 4180 for CSV format specification.

This reader supports records with arbitrary (variable) number of fields, multiline fields, custom separator and quote characters. It accepts CR, LF and CRLF sequence as record separators.

This reader provides its own buffering but does not perform decoding. The correct way to efficiently read CSV file with UTF-8 encoding is as follows:

 CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
 String[] header = reader.readRecord();
 List<String[]> records = reader.readAll();
 reader.close();
 


Constructor Summary
CSVReader(Reader reader)
          Creates new CSVReader with default separator and quote characters.
CSVReader(Reader reader, char separator, char quote)
          Creates new CSVReader with specified separator and quote characters.
 
Method Summary
 void close()
          Closes the stream.
 int getLineNumber()
          Returns current line number.
 int getRecordNumber()
          Returns current record number.
 List<String[]> readAll()
          Reads and returns all records or empty list if stream has ended.
 String readField()
          Reads and returns a single field of the current record or null if record has ended.
 String[] readRecord()
          Reads and returns a remaining fields of the current record or null if stream has ended.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVReader

public CSVReader(Reader reader)
Creates new CSVReader with default separator and quote characters.

Throws:
NullPointerException - if reader is null

CSVReader

public CSVReader(Reader reader,
                 char separator,
                 char quote)
Creates new CSVReader with specified separator and quote characters.

Throws:
NullPointerException - if reader is null
IllegalArgumentException - if separator or quote characters are invalid
Method Detail

getLineNumber

public int getLineNumber()
Returns current line number. Line numeration starts with 1 and counts new lines within record fields. Both CR and LF are counted as new lines, although CRLF sequence is counted only once. Line number points to new line after completion of the current record.


getRecordNumber

public int getRecordNumber()
Returns current record number. Record numeration starts with 1 and does not count new lines within record fields. Record number points to new record after completion of the current record.


readField

public String readField()
                 throws IOException
Reads and returns a single field of the current record or null if record has ended. Returns empty string if field is empty. This method does not advance to the next record - it keeps to return null.

Throws:
CSVFormatException - if input stream does not conform to the CSV format
IOException - If an I/O error occurs

readRecord

public String[] readRecord()
                    throws IOException
Reads and returns a remaining fields of the current record or null if stream has ended. Returns empty array (length 0) if all record fields were already read by readField() method. Returns array of length 1 with single empty string if record is empty (empty line). Returns empty strings for those fields that are empty. This method advances to the next record upon completion.

Throws:
CSVFormatException - if input stream does not conform to the CSV format
IOException - If an I/O error occurs

readAll

public List<String[]> readAll()
                       throws IOException
Reads and returns all records or empty list if stream has ended. Empty records are represented by arrays of length 1 with single empty string. Empty fields are represented by empty strings.

Throws:
CSVFormatException - if input stream does not conform to the CSV format
IOException - If an I/O error occurs

close

public void close()
           throws IOException
Closes the stream.

Specified by:
close in interface Closeable
Throws:
IOException - If an I/O error occurs


Copyright © 2013 Devexperts. All Rights Reserved.