Class CSVFileReader

java.lang.Object
  extended by CSVFile
      extended by CSVFileReader

public class CSVFileReader
extends CSVFile

CSVFileReader is a class derived from CSVFile used to parse an existing CSV file.

Adapted from a C++ original that is Copyright (C) 1999 Lucent Technologies.
Excerpted from 'The Practice of Programming' by Brian Kernighan and Rob Pike.

Included by permission of the Addison-Wesley web site, which says: "You may use this code for any purpose, as long as you leave the copyright notice and book citation attached".


Field Summary
protected  java.io.BufferedReader in
          The buffered reader linked to the CSV file to be read.
 
Fields inherited from class CSVFile
DEFAULT_FIELD_SEPARATOR, DEFAULT_TEXT_QUALIFIER, fieldSeparator, textQualifier
 
Constructor Summary
CSVFileReader(java.lang.String inputFileName)
          CSVFileReader constructor just need the name of the existing CSV file that will be read.
CSVFileReader(java.lang.String inputFileName, char sep)
          CSVFileReader constructor with a given field separator.
CSVFileReader(java.lang.String inputFileName, char sep, char qual)
          CSVFileReader constructor with given field separator and text qualifier.
 
Method Summary
 void close()
          Close the input CSV file.
protected  int handlePlainField(java.lang.String s, java.lang.StringBuffer sb, int i)
          Handles an unquoted field.
protected  int handleQuotedField(java.lang.String s, java.lang.StringBuffer sb, int i)
          Handles a quoted field.
 java.util.Vector<java.lang.String> readFields()
          Split the next line of the input CSV file into fields.
 
Methods inherited from class CSVFile
getFieldSeparator, getTextQualifier, setFieldSeparator, setTextQualifier
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

in

protected java.io.BufferedReader in
The buffered reader linked to the CSV file to be read.

Constructor Detail

CSVFileReader

public CSVFileReader(java.lang.String inputFileName)
              throws java.io.FileNotFoundException
CSVFileReader constructor just need the name of the existing CSV file that will be read.

Parameters:
inputFileName - The name of the CSV file to be opened for reading
Throws:
java.io.FileNotFoundException - If the file to be read does not exist

CSVFileReader

public CSVFileReader(java.lang.String inputFileName,
                     char sep)
              throws java.io.FileNotFoundException
CSVFileReader constructor with a given field separator.

Parameters:
inputFileName - The name of the CSV file to be opened for reading
sep - The field separator to be used; overwrites the default one
Throws:
java.io.FileNotFoundException - If the file to be read does not exist

CSVFileReader

public CSVFileReader(java.lang.String inputFileName,
                     char sep,
                     char qual)
              throws java.io.FileNotFoundException
CSVFileReader constructor with given field separator and text qualifier.

Parameters:
inputFileName - The name of the CSV file to be opened for reading
sep - The field separator to be used; overwrites the default one
qual - The text qualifier to be used; overwrites the default one
Throws:
java.io.FileNotFoundException - If the file to be read does not exist
Method Detail

readFields

public java.util.Vector<java.lang.String> readFields()
                                              throws java.io.IOException
Split the next line of the input CSV file into fields.

This is currently the most important function of the package.

Returns:
Vector of strings containing each field from the next line of the file
Throws:
java.io.IOException - If an error occurs while reading the new line from the file

close

public void close()
           throws java.io.IOException
Close the input CSV file.

Throws:
java.io.IOException - If an error occurs while closing the file

handleQuotedField

protected int handleQuotedField(java.lang.String s,
                                java.lang.StringBuffer sb,
                                int i)
Handles a quoted field.

Returns:
index of next separator

handlePlainField

protected int handlePlainField(java.lang.String s,
                               java.lang.StringBuffer sb,
                               int i)
Handles an unquoted field.

Returns:
index of next separator