edu.upc.dama.dex.io
Class CSVReader

java.lang.Object
  extended by edu.upc.dama.dex.io.CSVReader
All Implemented Interfaces:
RowReader

public class CSVReader
extends java.lang.Object
implements RowReader

A very simple CSV reader. Using the format RFC 4180.
Except: leading and trailing spaces, adjacent to CSV separator character, are trimmed.
Sample: ,   field   , trimmed to "field".
Default values:
Separator = ','
Quote Character = '"'
Allow Multilines = true

If you want to use an iterative style pattern, you might do something like this:

 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
 String [] nextLine;
 while ((nextLine = reader.readNext()) != null)
 {
   // nextLine[] is an array of values from the line
   System.out.println(nextLine[0] + nextLine[1] + "etc...");
 }
 
Or, if you might just want to load the whole lot into a List, just call readAll(). It will give you a List of String[] that you can iterate over.
 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
 List myEntries = reader.readAll();
 
You can use your own separators and quote characters. There are constructors that allows for supplying your own separator and quote characters. For example, if you are using a tab for your separator, you can do something like this:
 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');
 
And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:
 CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');
 

Author:
Sparsity Technologies

Field Summary
static boolean DEFAULT_ALLOW_MULTILINE
          The default value to allow multiline values in a CSV file if none is supplied to the constructor
static int DEFAULT_MAX_LINES
          The default line to end reading.
static char DEFAULT_QUOTE_CHARACTER
          The default quote character to use if none is supplied to the constructor.
static char DEFAULT_SEPARATOR
          The default separator to use if none is supplied to the constructor.
static int DEFAULT_SKIP_LINES
          The default line to start reading.
 
Constructor Summary
CSVReader(java.io.File file)
          Constructs CSVReader using a comma for the separator.
CSVReader(java.io.File file, char separator)
          Constructs CSVReader with supplied separator.
CSVReader(java.io.File file, char separator, char quotechar)
          Constructs CSVReader with supplied separator and quote char.
CSVReader(java.io.File file, char separator, char quotechar, boolean allowMultilines)
          Constructs CSVReader with supplied separator, quote char and line options.
CSVReader(java.io.File file, char separator, char quotechar, boolean allowMultilines, int startline)
          Constructs CSVReader with supplied separator, quote char and line options.
CSVReader(java.io.File file, char separator, char quotechar, boolean allowMultilines, int startline, int maxlines)
          Constructs CSVReader with supplied separator, quote char and line options.
CSVReader(java.io.Reader reader)
          Constructs CSVReader using a comma for the separator.
CSVReader(java.io.Reader reader, char separator)
          Constructs CSVReader with supplied separator.
CSVReader(java.io.Reader reader, char separator, char quotechar)
          Constructs CSVReader with supplied separator and quote char.
CSVReader(java.io.Reader reader, char separator, char quotechar, boolean allowMultilines)
          Constructs CSVReader with supplied separator, quote char and line options.
CSVReader(java.io.Reader reader, char separator, char quotechar, boolean allowMultilines, int startline)
          Constructs CSVReader with supplied separator, quote char and line options.
CSVReader(java.io.Reader reader, char separator, char quotechar, boolean allowMultilines, int startline, int maxlines)
          Constructs CSVReader with supplied separator, quote char and line options.
 
Method Summary
 void close()
          Closes the underlying reader.
 int getRow()
          The row number for the current row.
 java.util.List readAll()
          Reads the entire file into a List with each element being a String[] of tokens.
 java.lang.String[] readNext()
          Reads the next row as a string array.
 boolean reset()
          Moves the reader to the beginning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_ALLOW_MULTILINE

public static final boolean DEFAULT_ALLOW_MULTILINE
The default value to allow multiline values in a CSV file if none is supplied to the constructor

See Also:
Constant Field Values

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
The default separator to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_QUOTE_CHARACTER

public static final char DEFAULT_QUOTE_CHARACTER
The default quote character to use if none is supplied to the constructor.

See Also:
Constant Field Values

DEFAULT_SKIP_LINES

public static final int DEFAULT_SKIP_LINES
The default line to start reading.

See Also:
Constant Field Values

DEFAULT_MAX_LINES

public static final int DEFAULT_MAX_LINES
The default line to end reading.

See Also:
Constant Field Values
Constructor Detail

CSVReader

public CSVReader(java.io.Reader reader)
Constructs CSVReader using a comma for the separator.

Parameters:
reader - The reader to an underlying CSV source.

CSVReader

public CSVReader(java.io.File file)
          throws java.io.FileNotFoundException
Constructs CSVReader using a comma for the separator.

Parameters:
file - The file to an underlying CSV source.
Throws:
java.io.FileNotFoundException

CSVReader

public CSVReader(java.io.Reader reader,
                 char separator)
Constructs CSVReader with supplied separator.

Parameters:
reader - The reader to an underlying CSV source.
separator - The delimiter to use for separating entries.

CSVReader

public CSVReader(java.io.File file,
                 char separator)
          throws java.io.FileNotFoundException
Constructs CSVReader with supplied separator.

Parameters:
file - The file to an underlying CSV source.
separator - The delimiter to use for separating entries.
Throws:
java.io.FileNotFoundException

CSVReader

public CSVReader(java.io.Reader reader,
                 char separator,
                 char quotechar)
Constructs CSVReader with supplied separator and quote char.

Parameters:
reader - The reader to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements

CSVReader

public CSVReader(java.io.File file,
                 char separator,
                 char quotechar)
          throws java.io.FileNotFoundException
Constructs CSVReader with supplied separator and quote char.

Parameters:
file - The file to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
Throws:
java.io.FileNotFoundException

CSVReader

public CSVReader(java.io.Reader reader,
                 char separator,
                 char quotechar,
                 boolean allowMultilines)
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
reader - The reader to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields which has inquotted text with multiline values or no

CSVReader

public CSVReader(java.io.File file,
                 char separator,
                 char quotechar,
                 boolean allowMultilines)
          throws java.io.FileNotFoundException
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
file - The file to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields which has inquotted text with multiline values or no
Throws:
java.io.FileNotFoundException

CSVReader

public CSVReader(java.io.Reader reader,
                 char separator,
                 char quotechar,
                 boolean allowMultilines,
                 int startline)
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
reader - The reader to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields which has inquotted text
startline - The line number to skip for start reading with multiline values or no

CSVReader

public CSVReader(java.io.File file,
                 char separator,
                 char quotechar,
                 boolean allowMultilines,
                 int startline)
          throws java.io.FileNotFoundException
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
file - The file to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields which has inquotted text
startline - The line number to skip for start reading with multiline values or no
Throws:
java.io.FileNotFoundException

CSVReader

public CSVReader(java.io.Reader reader,
                 char separator,
                 char quotechar,
                 boolean allowMultilines,
                 int startline,
                 int maxlines)
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
reader - The reader to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields with multiline values
startline - The line number to skip for start reading
maxlines - The maximum number of lines to read

CSVReader

public CSVReader(java.io.File file,
                 char separator,
                 char quotechar,
                 boolean allowMultilines,
                 int startline,
                 int maxlines)
          throws java.io.FileNotFoundException
Constructs CSVReader with supplied separator, quote char and line options.

Parameters:
file - The file to an underlying CSV source.
separator - The delimiter to use for separating entries
quotechar - The character to use for quoted elements
allowMultilines - It allows fields with multiline values
startline - The line number to skip for start reading
maxlines - The maximum number of lines to read
Throws:
java.io.FileNotFoundException
Method Detail

readAll

public java.util.List readAll()
                       throws java.io.IOException
Reads the entire file into a List with each element being a String[] of tokens.

Returns:
A List of String[], with each String[] representing a line of the file.
Throws:
java.io.IOException - If bad things happen during the read

readNext

public java.lang.String[] readNext()
                            throws java.io.IOException
Reads the next row as a string array.

Specified by:
readNext in interface RowReader
Returns:
A string array with each comma-separated element as a separate entry.
Throws:
java.io.IOException - If bad things happen during the read.

reset

public boolean reset()
              throws java.io.IOException
Moves the reader to the beginning.

Restarts the reader. This interface only works when the CSVReader was built with a File, which can be reopen. If the instance was built with a common Reader, then it cannot be reopen and it will return false.

Specified by:
reset in interface RowReader
Returns:
true if the reader can be restarted, false otherwise.
Throws:
java.io.IOException - If the open fails.

close

public void close()
           throws java.io.IOException
Closes the underlying reader.

Specified by:
close in interface RowReader
Throws:
java.io.IOException - If the close fails.

getRow

public int getRow()
           throws java.io.IOException
The row number for the current row.

Specified by:
getRow in interface RowReader
Returns:
The current row number; 0 if there is no current row.
Throws:
java.io.IOException - If it fails.