edu.northwestern.at.utils.csv

## Class CSVFileReader

- java.lang.Object
- - edu.northwestern.at.utils.csv.CSVFile
  - - edu.northwestern.at.utils.csv.CSVFileReader

-     public class CSVFileReader
      extends CSVFile

  CSVFileReader reads and parses a delimiter separated text 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".

  Author:
    Brian Kernighan and Rob Pike (C++ original), Ian F. Darwin (translation into Java and removal of I/O), Ben Ballard (rewrote handleQuotedField to handle double qualifiers and for readability), Fabrizio Fazzino (added integration with CSVFile, handling of variable qualifier and ArrayList with explicit String type), Philip R. Burns. Allow character set encoding for files and List instead of Vector.

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected java.io.BufferedReader` | `in` Buffered reader for the CSV file to be read.

    - ### Fields inherited from class edu.northwestern.at.utils.csv.CSVFile

      `DEFAULT_QUALIFIER, DEFAULT_SEPARATOR, qualifier, separator`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CSVFileReader(java.lang.String inputFileName, java.lang.String encoding)` Create a CSV file reader given a file name and encoding.
    `CSVFileReader(java.lang.String inputFileName, java.lang.String encoding, char separator)` Create CSVFileReader with a given field separator.
    `CSVFileReader(java.lang.String inputFileName, java.lang.String encoding, char separator, char qualifier)` Create CSVFileReader with given field separator and qualifier character.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()` Close the input CSV file.
    `protected int` | `handlePlainField(java.lang.String s, java.lang.StringBuffer sb, int i)` Handles an unqualified field.
    `protected int` | `handleQuotedField(java.lang.String s, java.lang.StringBuffer sb, int i)` Handles a qualified field.
    `protected void` | `openInputFile(java.lang.String inputFileName, java.lang.String encoding)` Open input file.
    `java.util.List<java.lang.String>` | `readFields()` Split the next line of the input CSV file into fields.

    - ### Methods inherited from class edu.northwestern.at.utils.csv.CSVFile

      `getQualifier, getSeparator, setQualifier, setSeparator`

    - ### 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

      Buffered reader for the CSV file to be read.

  - ### Constructor Detail

    - #### CSVFileReader

          public CSVFileReader(java.lang.String inputFileName,
                       java.lang.String encoding)
                        throws java.io.IOException,
                               java.io.FileNotFoundException

      Create a CSV file reader given a file name and encoding.
      Parameters:
        `inputFileName` - Name of the CSV formatted input file.
        `encoding` - The character encoding for the file.
      Throws:
        `java.io.FileNotFoundException` - If input file does not exist.
        `java.io.IOException` - If input file cannot be opened.

    - #### CSVFileReader

          public CSVFileReader(java.lang.String inputFileName,
                       java.lang.String encoding,
                       char separator)
                        throws java.io.IOException,
                               java.io.FileNotFoundException

      Create CSVFileReader with a given field separator.
      Parameters:
        `inputFileName` - Name of the CSV formatted input file.
        `encoding` - Character encoding for the file.
        `separator` - Field separator.
      Throws:
        `java.io.FileNotFoundException` - If input file does not exist.
        `java.io.IOException` - If input file cannot be opened.

    - #### CSVFileReader

          public CSVFileReader(java.lang.String inputFileName,
                       java.lang.String encoding,
                       char separator,
                       char qualifier)
                        throws java.io.FileNotFoundException,
                               java.io.IOException

      Create CSVFileReader with given field separator and qualifier character.
      Parameters:
        `inputFileName` - Name of the CSV formatted input file.
        `encoding` - Character encoding for the file.
        `separator` - Field separator.
        `qualifier` - Qualified character. Ascii 0 means don't use a qualifier.
      Throws:
        `java.io.FileNotFoundException` - If input file does not exist.
        `java.io.IOException` - If input file cannot be opened.

  - ### Method Detail

    - #### openInputFile

          protected void openInputFile(java.lang.String inputFileName,
                           java.lang.String encoding)
                                throws java.io.IOException

      Open input file.
      Parameters:
        `inputFileName` - Name of the CSV formatted input file.
        `encoding` - Character encoding for the file.
      Throws:
        `java.io.IOException`

    - #### readFields

          public java.util.List<java.lang.String> readFields()
                                                      throws java.io.IOException

      Split the next line of the input CSV file into fields.
      Returns:
        List 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 qualified field.
      Parameters:
        `s` - Input string.
        `sb` - Output string buffer.
        `i` - Current offset in string s.

    - #### handlePlainField

          protected int handlePlainField(java.lang.String s,
                             java.lang.StringBuffer sb,
                             int i)

      Handles an unqualified field.
      Parameters:
        `s` - Input string.
        `sb` - Output string buffer.
        `i` - Current offset in string s.
      Returns:
        index of next separator.