Class CsvReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable, Readable

    public class CsvReader
    extends LineNumberReader
    A CsvReader instance is used read text files with CSV (comma separated values) format.

    This reader interprets each line of a text file as record (also empty lines). The fields of the record are delimited by a single separator character which must not necessarily be a comma.

    Version:
    1.1
    • Constructor Detail

      • CsvReader

        public CsvReader​(Reader reader,
                         char[] separators)
        Constructs a CSV reader using the given column separator characters. The new reader will not ignore empty lines and does not recognize comment lines.
        Parameters:
        reader - the underlying reader to be wrapped
        separators - the column separator characters
      • CsvReader

        public CsvReader​(Reader reader,
                         char[] separators,
                         boolean ignoreEmptyLines,
                         String commentPrefix)
        Constructs a CSV reader using the given column separator characters and the format properties.
        Parameters:
        reader - the underlying reader to be wrapped
        separators - the column separator characters
        ignoreEmptyLines - if true, empty lines are ignored
        commentPrefix - if not null, the prefix string for comment lines
    • Method Detail

      • getSeparators

        public final char[] getSeparators()
        Returns:
        The column separator characters.
      • ignoresEmptyLines

        public final boolean ignoresEmptyLines()
        Returns:
        Are empty lines ignored?
      • getCommentPrefix

        public final String getCommentPrefix()
        Gets the comment prefix string (if any).
        Returns:
        The comment prefix string, if any. null otherwise.
      • readRecord

        public String[] readRecord()
                            throws IOException
        Reads a record info from the database. A record is represented by a complete line in the CSV formatted text file.

        Leading and trailing whitespaces removed from each column value. For empty lines, the method returns an array of the length zero. The method returns null if the end of file has been reached.

        Returns:
        a record containing the tokens delimited by the separator character passed to the constructor
        Throws:
        IOException - if an I/O error occurs
      • readStringRecords

        public List<String[]> readStringRecords()
                                         throws IOException
        Reads the complete file from the current position on. If the readLineRecord has not previously been called the method reads all records from the beginning of the file. For empty files, the method returns an vector having a zero size.
        Returns:
        an vector of String[] records containing the tokens delimitted by the separator character passed to the constructor
        Throws:
        IOException - if an I/O error occurs