Class CSVFileReader

java.lang.Object
org.evolvis.tartools.csvfile.CSVFile
org.evolvis.tartools.csvfile.CSVFileReader
Direct Known Subclasses:
SSVFileReader

public class CSVFileReader extends CSVFile
CSVFileReader is a class derived from CSVFile used to parse an existing CSV file.
Author:
Brian Kernighan and Rob Pike (C++ original), Ian F. Darwin (translation into Java and removal of I/O), Ben Ballard (double quote handling and readability), Fabrizio Fazzino (CSVFile integration, textQualifier handling, Vectors), Michael “Mikel” Klink (de-genericisation, etc.)
  • Field Details

    • in

      protected final BufferedReader in
      The buffered reader linked to the CSV file to be read.
    • line

      protected String line
      The currently being read input line.
  • Constructor Details

    • CSVFileReader

      public CSVFileReader(String inputFileName) throws FileNotFoundException
      CSVFileReader constructor just needing the name of the existing CSV file to read.
      Parameters:
      inputFileName - The name of the CSV file to be opened for reading
      Throws:
      FileNotFoundException - if the file to be read does not exist
    • CSVFileReader

      public CSVFileReader(Reader reader)
      CSVFileReader constructor just needing a reader for the CSV data that will be read.
      Parameters:
      reader - The Reader for reading CSV data
    • CSVFileReader

      public CSVFileReader(String inputFileName, String charsetName) throws FileNotFoundException, UnsupportedEncodingException
      CSVFileReader constructor just needing the name and encoding of the existing CSV file that will be read.
      Parameters:
      inputFileName - The name of the CSV file to be opened for reading
      charsetName - The name of a supported charset
      Throws:
      FileNotFoundException - if the file to be read does not exist
      UnsupportedEncodingException - if the named charset is not supported
    • CSVFileReader

      public CSVFileReader(InputStream stream, String charsetName) throws UnsupportedEncodingException
      CSVFileReader constructor just needing an InputStream and encoding for the CSV data that will be read.
      Parameters:
      stream - The InputStream for reading CSV data
      charsetName - The name of a supported charset
      Throws:
      UnsupportedEncodingException - if the named charset is not supported
    • CSVFileReader

      public CSVFileReader(String inputFileName, char sep) throws 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:
      FileNotFoundException - if the file to be read does not exist
    • CSVFileReader

      public CSVFileReader(Reader reader, char sep)
      CSVFileReader constructor with a given field separator.
      Parameters:
      reader - The Reader for reading CSV data
      sep - The field separator to be used; overwrites the default one
    • CSVFileReader

      public CSVFileReader(String inputFileName, String charsetName, char sep) throws FileNotFoundException, UnsupportedEncodingException
      CSVFileReader constructor with a given field separator.
      Parameters:
      inputFileName - The name of the CSV file to be opened for reading
      charsetName - The name of a supported charset
      sep - The field separator to be used; overwrites the default one
      Throws:
      FileNotFoundException - if the file to be read does not exist
      UnsupportedEncodingException - if the named charset is not supported
    • CSVFileReader

      public CSVFileReader(InputStream stream, String charsetName, char sep) throws UnsupportedEncodingException
      CSVFileReader constructor with a given field separator.
      Parameters:
      stream - The InputStream for reading CSV data
      charsetName - The name of a supported charset
      sep - The field separator to be used; overwrites the default one
      Throws:
      UnsupportedEncodingException - if the named charset is not supported
    • CSVFileReader

      public CSVFileReader(String inputFileName, char sep, char qual) throws 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:
      FileNotFoundException - if the file to be read does not exist
    • CSVFileReader

      public CSVFileReader(Reader reader, char sep, char qual)
      CSVFileReader constructor with given field separator and text qualifier.
      Parameters:
      reader - The Reader for reading CSV data
      sep - The field separator to be used; overwrites the default one
      qual - The text qualifier to be used; overwrites the default one
    • CSVFileReader

      public CSVFileReader(String inputFileName, String charsetName, char sep, char qual) throws FileNotFoundException, UnsupportedEncodingException
      CSVFileReader constructor with given field separator and text qualifier.
      Parameters:
      inputFileName - The name of the CSV file to be opened for reading
      charsetName - The name of a supported charset
      sep - The field separator to be used; overwrites the default one
      qual - The text qualifier to be used; overwrites the default one
      Throws:
      FileNotFoundException - if the file to be read does not exist
      UnsupportedEncodingException - if the named charset is not supported
    • CSVFileReader

      public CSVFileReader(InputStream stream, String charsetName, char sep, char qual) throws UnsupportedEncodingException
      CSVFileReader constructor with given field separator and text qualifier.
      Parameters:
      stream - The InputStream for reading CSV data
      charsetName - The name of a supported charset
      sep - The field separator to be used; overwrites the default one
      qual - The text qualifier to be used; overwrites the default one
      Throws:
      UnsupportedEncodingException - if the named charset is not supported
  • Method Details

    • readFields

      public List<String> readFields() throws IOException
      Splits the next line of the input CSV file into fields. This is currently the most important function of the package. It can read a subsequent line from the input stream if necessary due to a newline inside a quoted field.
      Returns:
      List of String containing each field from the next line of the file
      Throws:
      IOException - if an error occurs while reading the new line from the file
    • readFields

      public List<String> readFields(String firstLine) throws IOException
      Splits the next line of the input CSV file into fields. This is currently the most important function of the package. It can read a subsequent line from the input stream if necessary due to a newline inside a quoted field.
      Parameters:
      firstLine - result of in.readLine() if called by parent for preparation already (can only happen if instantiated with a BufferedReader)
      Returns:
      List of String containing each field from the next line of the file
      Throws:
      IOException - if an error occurs while reading the new line from the file
    • fieldIsQuoted

      protected boolean fieldIsQuoted(int i)
      Checks whether the current input line field is quoted.
      Parameters:
      i - Offset into the input line denoting start of field
      Returns:
      false if the field is not quoted or EOL is reached
    • addField

      protected void addField(List<String> fields, String field)
      Adds extracted field to list of fields. Child classes can override to add post-processing.
      Parameters:
      fields - list of fields to add field to
      field - raw extracted String
    • close

      public void close() throws IOException
      Closes the input CSV file.
      Throws:
      IOException - if an error occurs while closing the file
    • handleQuotedField

      protected int handleQuotedField(StringBuilder sb, int i) throws IOException
      Handles a quoted field. Note: does not handle filler between closing char and end of field/line well.
      Parameters:
      sb - the StringBuilder to add the resulting field into
      i - the offset of the first supposed character of the field (past the quote)
      Returns:
      index of next separator
      Throws:
      IOException - if input cannot be read
    • handlePlainField

      protected int handlePlainField(StringBuilder sb, int i)
      Handles an unquoted field.
      Parameters:
      sb - the StringBuilder to add the resulting field into
      i - the offset of the first supposed character of the field
      Returns:
      index of next separator