Package org.kissweb

Class DelimitedFileReader

java.lang.Object
org.kissweb.DelimitedFileReader
All Implemented Interfaces:
AutoCloseable

public class DelimitedFileReader extends Object implements AutoCloseable
Read and parse a CSV file. This class implements the AutoCloseable interface.

This code attempts to conform to RFC 4180.

Fields can be accessed sequentially, by index, or by field name. Field names are obtained at the first record of the CSV file.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Open an existing CSV file with the default delimiter (,) and quote (") characters,
    DelimitedFileReader(File f, char delimiter, char quote)
    Open an existing CSV file with the specified delimiter and quote character.
    Open an existing CSV file with the default delimiter (,) and quote (") characters,
    DelimitedFileReader(String name, char delimiter, char quote)
    Open an existing CSV file with the specified delimiter and quote character.
  • Method Summary

    Modifier and Type
    Method
    Description
    checkHeaders(String[] colNames)
    Checks if the given column titles exist in the delimited file.
    void
    Close the CSV file.
    int
    getDate(int item)
    Return the date as an int at the indicated field number.
    int
    Return the date as an int at the indicated field label.
    double
    getDouble(int i)
    Gets the double value of field number i.
    double
    Get the double value of the field indicated by fld.
    int
    getInt(int item)
    Return the int at the indicated field number.
    int
    Return the int at the field with the indicated label.
    Return the original row / line last read.
    getString(int item)
    Return the String at the indicated field number.
    Return the String at the field with the indicated label.
    boolean
    hasHeader(String header)
    Checks if the given column header exists in the delimited file.
    static void
    main(String[] args)
     
    void
    Move the record pointer back to the beginning of the CSV file.
    int
    Returns the int date at the next sequential field.
    double
    Returns the next String field.
    int
    Returns the next int field.
    boolean
    Read and parse the next row in the CSV file.
    Returns the next String field.
    void
    Read the first row and map column title names to indexes.
    void
    setFieldCountCheck(int check)
    Optional method used to set the expected number of fields in each row.
    int
    Returns the number of columns / fields in the current row / record.
    void
    Skip the next line in the CSV file.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DelimitedFileReader

      public DelimitedFileReader(File f, char delimiter, char quote) throws FileNotFoundException
      Open an existing CSV file with the specified delimiter and quote character.
      Parameters:
      f -
      delimiter -
      quote -
      Throws:
      FileNotFoundException
    • DelimitedFileReader

      public DelimitedFileReader(File f) throws FileNotFoundException
      Open an existing CSV file with the default delimiter (,) and quote (") characters,
      Parameters:
      f -
      Throws:
      FileNotFoundException
    • DelimitedFileReader

      public DelimitedFileReader(String name) throws FileNotFoundException
      Open an existing CSV file with the default delimiter (,) and quote (") characters,
      Parameters:
      name -
      Throws:
      FileNotFoundException
    • DelimitedFileReader

      public DelimitedFileReader(String name, char delimiter, char quote) throws FileNotFoundException
      Open an existing CSV file with the specified delimiter and quote character.
      Parameters:
      name -
      delimiter -
      quote -
      Throws:
      FileNotFoundException
  • Method Details

    • close

      public void close()
      Close the CSV file.

      Note that this method will be called automatically if the try-with-resource Java facility is utilized.
      Specified by:
      close in interface AutoCloseable
    • getDouble

      public double getDouble(int i)
      Gets the double value of field number i.
      Parameters:
      i - field number, index origin 0
      Returns:
    • getDouble

      public double getDouble(String fld)
      Get the double value of the field indicated by fld.
      Parameters:
      fld -
      Returns:
    • moveToStart

      public void moveToStart()
      Move the record pointer back to the beginning of the CSV file. If the first record are the field names it will need to be re-read.
    • skipLine

      public void skipLine() throws IOException, Exception
      Skip the next line in the CSV file.
      Throws:
      IOException
      Exception
    • readHeader

      public void readHeader()
      Read the first row and map column title names to indexes. Column titles are case-insensitive.
    • hasHeader

      public boolean hasHeader(String header)
      Checks if the given column header exists in the delimited file. Note that this only works after readHeader() has been called.
      Parameters:
      header - the header to check for
      Returns:
      true if the header exists, false otherwise
    • checkHeaders

      public ArrayList<String> checkHeaders(String[] colNames)
      Checks if the given column titles exist in the delimited file. Note that this only works after readHeader() has been called.
      Parameters:
      colNames - an array of column names to check
      Returns:
      a list of column names that do not exist in the nameMap, or null if all column names exist
    • getRow

      public String getRow()
      Return the original row / line last read.
      Returns:
    • nextLine

      public boolean nextLine() throws IOException, Exception
      Read and parse the next row in the CSV file.
      Returns:
      true if another line was found, false of end-of-file
      Throws:
      IOException
      Exception
    • size

      public int size()
      Returns the number of columns / fields in the current row / record.
      Returns:
    • nextString

      public String nextString()
      Returns the next String field. This method is useful when reading the fields in sequence rather than by index.
      Returns:
    • getString

      public String getString(int item)
      Return the String at the indicated field number.
      Parameters:
      item - field number, index origin 0
      Returns:
    • getString

      public String getString(String fld)
      Return the String at the field with the indicated label.
      Parameters:
      fld -
      Returns:
      See Also:
    • nextInt

      public int nextInt()
      Returns the next int field. This method is useful when reading the fields in sequence rather than by index.
      Returns:
    • nextDouble

      public double nextDouble()
      Returns the next String field. This method is useful when reading the fields in sequence rather than by index.
      Returns:
    • getInt

      public int getInt(int item)
      Return the int at the indicated field number.
      Parameters:
      item - field number, index origin 0
      Returns:
    • getInt

      public int getInt(String fld)
      Return the int at the field with the indicated label.
      Parameters:
      fld -
      Returns:
      See Also:
    • nextDate

      public int nextDate()
      Returns the int date at the next sequential field.
      Returns:
      date as int YYYYMMDD
    • getDate

      public int getDate(int item)
      Return the date as an int at the indicated field number.
      Parameters:
      item - field number, index origin 0
      Returns:
      date as int YYYYMMDD
    • getDate

      public int getDate(String fld)
      Return the date as an int at the indicated field label.
      Parameters:
      fld -
      Returns:
      date as int YYYYMMDD
      See Also:
    • setFieldCountCheck

      public void setFieldCountCheck(int check)
      Optional method used to set the expected number of fields in each row. If this is set and the number of columns is not what is expected, nextLine() will throw an exception.
      Parameters:
      check -
    • main

      public static void main(String[] args) throws Exception
      Throws:
      Exception