Class CSVFileReader


  • 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.)
    • Constructor Detail

      • 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​(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​(InputStream stream,
                             String charsetName,
                             char sep)
                      throws UnsupportedEncodingException
        CSVFileReader constructor with a given field separator.
        Parameters:
        stream - The stream 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 Detail

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