net.sf.csv4j
Class CSVReader

java.lang.Object
  extended by net.sf.csv4j.CSVReader

public class CSVReader
extends Object

Reads and parses CSV lines from an input source. Correctly handles quoted fields which contain line terminators. The CSVReader is designed to be similar to Reader classes and in particular is similar to the LineNumberReader in terms of its API.

Since:
1.0
Author:
Shawn Boyce

Constructor Summary
CSVReader(Reader reader)
          Constructs a CSV reader with the default options.
CSVReader(Reader reader, char comment)
          Constructs a CSV reader with the specified options.
CSVReader(Reader reader, char delimiter, char comment)
          Constructs a CSV reader with the specified options.
 
Method Summary
 void close()
          Closes the input
 long countFields()
          Reads and parses the next CSV line from the input.
 String getCurrentLine()
           
 int getLineNumber()
          Returns the current line number.
 String readCSVLine()
          Read a line of CSV text.
 String readJSonLine()
          Reads and parses the next CSV line from the input.
 List<String> readLine()
          Reads and parses the next CSV line from the input.
 List<String> readLine(boolean includeComment)
          Reads and parses the next CSV line from the input.
 
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)
Constructs a CSV reader with the default options.

Parameters:
reader - input to read from.

CSVReader

public CSVReader(Reader reader,
                 char comment)
Constructs a CSV reader with the specified options.

Parameters:
reader - input to read from. A BufferedReader is recommended for better performance. Note that a LineNumberReader should not be used since it will not be able to track the line numbers correctly (newlines can be escaped in a CSV file).
comment - character indicating line is a comment and should be ignored

CSVReader

public CSVReader(Reader reader,
                 char delimiter,
                 char comment)
Constructs a CSV reader with the specified options.

Parameters:
reader - input to read from. A BufferedReader is recommended for better performance. Note that a LineNumberReader should not be used since it will not be able to track the line numbers correctly (newlines can be escaped in a CSV file).
comment - character indicating line is a comment and should be ignored
delimiter - field delimiter character
Method Detail

getLineNumber

public int getLineNumber()
Returns the current line number. Number only changes after readLine() is invoked.

Returns:
current line number

getCurrentLine

public String getCurrentLine()
Returns:
the currentLine

readLine

public List<String> readLine()
                      throws IOException,
                             ParseException
Reads and parses the next CSV line from the input. Comment lines will be ignored.

Returns:
null if EOF reached
Throws:
IOException - if an error occurs reading the input
ParseException - if an error occurs during CSV parsing

readLine

public List<String> readLine(boolean includeComment)
                      throws IOException,
                             ParseException
Reads and parses the next CSV line from the input. Comment lines will be ignored.

Parameters:
includeComment - true to include lines starting with comment char
Returns:
null if EOF reached
Throws:
IOException - if an error occurs reading the input
ParseException - if an error occurs during CSV parsing

countFields

public long countFields()
                 throws IOException,
                        ParseException
Reads and parses the next CSV line from the input. Comment lines will be ignored.

Returns:
the number of fields, -1 if EOF reached
Throws:
IOException - if an error occurs reading the input
ParseException - if an error occurs during CSV parsing

readJSonLine

public String readJSonLine()
                    throws IOException,
                           ParseException
Reads and parses the next CSV line from the input. Comment lines will be ignored.

Returns:
json line
Throws:
IOException - if an error occurs reading the input
ParseException - if an error occurs during CSV parsing

close

public void close()
           throws IOException
Closes the input

Throws:
IOException - if an I/O error occurs

readCSVLine

public String readCSVLine()
                   throws IOException
Read a line of CSV text. A line is terminated by newline ('\n'), carriage return ('\r'), or both ('\r\n') unless they are enclosed within double quotes. Line is also terminated by the EOF.

Returns:
CSV line without line terminator characters; null returned if EOF reached
Throws:
IOException - if an IO error occurs


Copyright © 2014. All Rights Reserved.