Package nl.saxion.app

Class CsvReader

java.lang.Object
nl.saxion.app.CsvReader

public class CsvReader extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
    CsvReader(String filename)
    Create a new reader for a CSV (comma separated values) file.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    getDouble(int column)
    Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a double.
    int
    getInt(int column)
    Reads the value at the given column from the currently loaded row (see: `loadRow()`) as an integer.
    getString(int column)
    Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a String.
    boolean
    isEmpty(int column)
    Checks if the value at the given column is empty.
    boolean
    Loads the next csv line into memory.
    void
    setQuote(char quote)
    Set the quote character.
    void
    setSeparator(char separator)
    Set the field separator character.
    void
    Ignore a row.

    Methods inherited from class java.lang.Object

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

    • CsvReader

      public CsvReader(String filename)
      Create a new reader for a CSV (comma separated values) file.
      Parameters:
      filename - The name (and path) of the .csv file to read.
  • Method Details

    • setSeparator

      public void setSeparator(char separator)
      Set the field separator character. Default to ';' (semicolon).
      Parameters:
      separator - The separator character (default ';')
    • setQuote

      public void setQuote(char quote)
      Set the quote character. Default to '"' (double quote).
      Parameters:
      quote - The quotation character (default '"')
    • skipRow

      public void skipRow()
      Ignore a row. Mostly useful for skipping the header line of a csv file.
    • loadRow

      public boolean loadRow()
      Loads the next csv line into memory. After a line has been read, column values can be retrieve using the getString/getInt/getDouble methods.
      Returns:
      A boolean indicating if a line was read (true) or we're at the end of file (false).
    • isEmpty

      public boolean isEmpty(int column)
      Checks if the value at the given column is empty.
      Parameters:
      column - The number of the column to retrieve, where 0 is the left-most column.
      Returns:
      True if the column has no value in it, false otherwise.
      Throws:
      Error - In case no row is loaded.
      IndexOutOfBoundsException - In case the column does not exist in the row.
    • getString

      public String getString(int column)
      Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a String.
      Parameters:
      column - The number of the column to retrieve, where 0 is the left-most column.
      Returns:
      The value.
      Throws:
      Error - In case no row is loaded.
      IndexOutOfBoundsException - In case the column does not exist in the row.
    • getInt

      public int getInt(int column)
      Reads the value at the given column from the currently loaded row (see: `loadRow()`) as an integer.
      Parameters:
      column - The number of the column to retrieve, where 0 is the left-most column.
      Returns:
      The value.
      Throws:
      Error - In case no row is loaded, or when the field is not an integer.
      IndexOutOfBoundsException - In case the column does not exist in the row.
    • getDouble

      public double getDouble(int column)
      Reads the value at the given column from the currently loaded row (see: `loadRow()`) as a double.
      Parameters:
      column - The number of the column to retrieve, where 0 is the left-most column.
      Returns:
      The value.
      Throws:
      Error - In case no row is loaded.
      IndexOutOfBoundsException - In case the column does not exist in the row.