Class CSVReader

    • Constructor Detail

      • CSVReader

        public CSVReader​(@Nonnull @WillCloseWhenClosed
                         Reader aReader)
        Constructs CSVReader using a comma for the separator.
        Parameters:
        aReader - the reader to an underlying CSV source.
      • CSVReader

        public CSVReader​(@Nonnull @WillCloseWhenClosed
                         Reader aReader,
                         boolean bKeepCR)
        Constructs CSVReader using a comma for the separator.
        Parameters:
        aReader - the reader to an underlying CSV source.
        bKeepCR - true to keep carriage returns in data read, false otherwise
      • CSVReader

        public CSVReader​(@Nonnull @WillCloseWhenClosed
                         Reader aReader,
                         @Nonnull
                         CSVParser aParser,
                         boolean bKeepCR)
        Constructs CSVReader with supplied CSVParser.
        Parameters:
        aReader - the reader to an underlying CSV source.
        aParser - the parser to use to parse input
        bKeepCR - true to keep carriage returns in data read, false otherwise
    • Method Detail

      • getParser

        @Nonnull
        public CSVParser getParser()
        Returns:
        the CSVParser used by the reader.
      • getSeparatorChar

        public char getSeparatorChar()
        Returns:
        The default separator for this parser.
      • setSeparatorChar

        @Nonnull
        public CSVReader setSeparatorChar​(char cSeparator)
        Sets the delimiter to use for separating entries.
        Parameters:
        cSeparator - the delimiter to use for separating entries
        Returns:
        this
      • getQuoteChar

        public char getQuoteChar()
        Returns:
        The default quotation character for this parser.
      • setQuoteChar

        @Nonnull
        public CSVReader setQuoteChar​(char cQuoteChar)
        Sets the character to use for quoted elements.
        Parameters:
        cQuoteChar - the character to use for quoted element.
        Returns:
        this
      • getEscapeChar

        public char getEscapeChar()
        Returns:
        The default escape character for this parser.
      • setEscapeChar

        @Nonnull
        public CSVReader setEscapeChar​(char cEscapeChar)
        Sets the character to use for escaping a separator or quote.
        Parameters:
        cEscapeChar - the character to use for escaping a separator or quote.
        Returns:
        this
      • isStrictQuotes

        public boolean isStrictQuotes()
        Returns:
        The default strictQuotes setting for this parser.
      • setStrictQuotes

        @Nonnull
        public CSVReader setStrictQuotes​(boolean bStrictQuotes)
        Sets the strict quotes setting - if true, characters outside the quotes are ignored.
        Parameters:
        bStrictQuotes - if true, characters outside the quotes are ignored
        Returns:
        this
      • isIgnoreLeadingWhiteSpace

        public boolean isIgnoreLeadingWhiteSpace()
        Returns:
        The default ignoreLeadingWhiteSpace setting for this parser.
      • setIgnoreLeadingWhiteSpace

        @Nonnull
        public CSVReader setIgnoreLeadingWhiteSpace​(boolean bIgnoreLeadingWhiteSpace)
        Sets the ignore leading whitespace setting - if true, white space in front of a quote in a field is ignored.
        Parameters:
        bIgnoreLeadingWhiteSpace - if true, white space in front of a quote in a field is ignored
        Returns:
        this
      • isIgnoreQuotations

        public boolean isIgnoreQuotations()
        Returns:
        the default ignoreQuotation setting for this parser.
      • setIgnoreQuotations

        @Nonnull
        public CSVReader setIgnoreQuotations​(boolean bIgnoreQuotations)
        Sets the ignore quotations mode - if true, quotations are ignored.
        Parameters:
        bIgnoreQuotations - if true, quotations are ignored
        Returns:
        this
      • getSkipLines

        @Nonnegative
        public int getSkipLines()
        Returns the number of lines in the csv file to skip before processing. This is useful when there is miscellaneous data at the beginning of a file.
        Returns:
        the number of lines in the csv file to skip before processing.
      • setSkipLines

        @Nonnull
        public CSVReader setSkipLines​(@Nonnegative
                                      int nSkipLines)
        Sets the line number to skip for start reading.
        Parameters:
        nSkipLines - the line number to skip for start reading.
        Returns:
        this
      • isKeepCarriageReturns

        public boolean isKeepCarriageReturns()
        Returns if the reader will keep carriage returns found in data or remove them.
        Returns:
        true if reader will keep carriage returns, false otherwise.
      • isVerifyReader

        public boolean isVerifyReader()
        Returns if the CSVReader will verify the reader before each read.
        By default the value is true which is the functionality for version 3.0. If set to false the reader is always assumed ready to read - this is the functionality for version 2.4 and before.
        The reason this method was needed was that certain types of Readers would return false for its ready() method until a read was done (namely readers created using Channels). This caused opencsv not to read from those readers.
        Source: https://sourceforge.net/p/opencsv/bugs/108/
        Returns:
        true if CSVReader will verify the reader before reads. false otherwise.
      • setVerifyReader

        @Nonnull
        public CSVReader setVerifyReader​(boolean bVerifyReader)
        Checks to see if the CSVReader should verify the reader state before reads or not. This should be set to false if you are using some form of asynchronous reader (like readers created by the java.nio.* classes). The default value is true.
        Parameters:
        bVerifyReader - true if CSVReader should verify reader before each read, false otherwise.
        Returns:
        this
      • readAll

        public void readAll​(@Nonnull
                            Consumer<? super ICommonsList<String>> aLineConsumer)
                     throws IOException
        Reads the entire file line by line and invoke a callback for each line.
        Parameters:
        aLineConsumer - The consumer that is invoked for every line. May not be null.
        Throws:
        IOException - if bad things happen during the read
      • readNext

        @Nullable
        public ICommonsList<String> readNext()
                                      throws IOException
        Reads the next line from the buffer and converts to a string array.
        Returns:
        a string array with each comma-separated element as a separate entry.
        Throws:
        IOException - if bad things happen during the read