Class CSVReader

java.lang.Object
com.helger.commons.csv.CSVReader
All Implemented Interfaces:
ICommonsIterable<ICommonsList<String>>, Closeable, AutoCloseable, Iterable<ICommonsList<String>>

public class CSVReader extends Object implements Closeable, ICommonsIterable<ICommonsList<String>>
A very simple CSV reader released under a commercial-friendly license.
Author:
Glen Smith, Philip Helger
  • Constructor Details

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

    • 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

      Reads the entire file into a list with each element being a list of String of tokens.
      Returns:
      a list of list of String, with each inner list of String representing a line of the file.
      Throws:
      IOException - if bad things happen during the read
    • 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
    • close

      public void close() throws IOException
      Closes the underlying reader.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if the close fails
    • iterator

      @Nonnull public Iterator<ICommonsList<String>> iterator()
      Creates an Iterator for processing the csv data.
      Specified by:
      iterator in interface Iterable<ICommonsList<String>>
      Returns:
      an ICommonsList<String> iterator.