Class DelimitedReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class DelimitedReader
    extends Object
    implements Closeable
    A utility for reading a file consisting of strings separated by a delimiter character, such as the classic comma-separated-value (CSV) or tab-delimited file formats. Strings are extracted with leading and trailing white space removed. Embedded white space characters are preserved.
    • Constructor Detail

      • DelimitedReader

        public DelimitedReader​(File file,
                               char delimiter)
                        throws IOException
        Open a file and prepare to read it using the specified delimiter character.
        Parameters:
        file - a valid file reference
        delimiter - a delimiter character
        Throws:
        IOException - in the event of an unsuccessful I/O operation
      • DelimitedReader

        public DelimitedReader​(InputStream ins,
                               char delimiter)
                        throws IOException
        Prepares to read an input stream using the specified delimiter character.
        Parameters:
        ins - a valid input stream reference
        delimiter - a delimiter character
        Throws:
        IOException - in the event of an unsuccessful I/O operation
    • Method Detail

      • readStrings

        public List<String> readStrings()
                                 throws IOException
        Read a row of strings from the file
        Returns:
        a list of strings (potentially empty)
        Throws:
        IOException - in the event of an unsuccessful I/O operation.
      • readStrings

        public int readStrings​(List<String> sList)
                        throws IOException
        Read a row of strings from the file, storing the results in a reusable list. Each call to this routine clears any content that may already be in the list before extracting it from the file.
        Parameters:
        sList - a list in which the strings will be stored
        Returns:
        the number of strings that were stored in the list; zero at the end of the file.
        Throws:
        IOException - in the event of an unsuccessful I/O operation.
      • getLineNumber

        public int getLineNumber()
        Gets the current position in the file as a line number.
        Returns:
        a value from 1 to the number of lines in the file.