Class CSVParser

java.lang.Object
com.landawn.abacus.util.CSVParser

public class CSVParser extends Object
A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.
Author:
Glen Smith, Rainer Pruy
  • Field Details

    • DEFAULT_SEPARATOR

      public static final char DEFAULT_SEPARATOR
      The default separator to use if none is supplied to the constructor.
      See Also:
    • INITIAL_READ_SIZE

      public static final int INITIAL_READ_SIZE
      The average size of a line read by opencsv (used for setting the size of StringBuilders).
      See Also:
    • READ_BUFFER_SIZE

      public static final int READ_BUFFER_SIZE
      In most cases we know the size of the line we want to read. In that case we will set the initial read to that plus an buffer size.
      See Also:
    • DEFAULT_QUOTE_CHARACTER

      public static final char DEFAULT_QUOTE_CHARACTER
      The default quote character to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_ESCAPE_CHARACTER

      public static final char DEFAULT_ESCAPE_CHARACTER
      The default escape character to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_STRICT_QUOTES

      public static final boolean DEFAULT_STRICT_QUOTES
      The default strict quote behavior to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_IGNORE_LEADING_WHITESPACE

      public static final boolean DEFAULT_IGNORE_LEADING_WHITESPACE
      The default leading whitespace behavior to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_IGNORE_QUOTATIONS

      public static final boolean DEFAULT_IGNORE_QUOTATIONS
      If the quote character is set to null then there is no quote character.
      See Also:
    • NULL_CHARACTER

      public static final char NULL_CHARACTER
      This is the "null" character - if a value is set to this then it is ignored.
      See Also:
  • Constructor Details

    • CSVParser

      public CSVParser()
      Constructs CSVParser using a comma for the separator.
    • CSVParser

      public CSVParser(char separator)
      Constructs CSVParser with supplied separator.
      Parameters:
      separator - The delimiter to use for separating entries.
    • CSVParser

      public CSVParser(char separator, char quotechar)
      Constructs CSVParser with supplied separator and quote char.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
    • CSVParser

      public CSVParser(char separator, char quotechar, char escape)
      Constructs CSVParser with supplied separator and quote char.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
      escape - The character to use for escaping a separator or quote
    • CSVParser

      public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes)
      Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" flag.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
      escape - The character to use for escaping a separator or quote
      strictQuotes - If true, characters outside the quotes are ignored
    • CSVParser

      public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace)
      Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
      escape - The character to use for escaping a separator or quote
      strictQuotes - If true, characters outside the quotes are ignored
      ignoreLeadingWhiteSpace - If true, white space in front of a quote in a field is ignored
    • CSVParser

      public CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace, boolean ignoreQuotations)
      Constructs CSVParser with supplied separator and quote char. Allows setting the "strict quotes" and "ignore leading whitespace" flags.
      Parameters:
      separator - The delimiter to use for separating entries
      quotechar - The character to use for quoted elements
      escape - The character to use for escaping a separator or quote
      strictQuotes - If true, characters outside the quotes are ignored
      ignoreLeadingWhiteSpace - If true, white space in front of a quote in a field is ignored
      ignoreQuotations - If true, treat quotations like any other character.
  • Method Details

    • getSeparator

      public char getSeparator()
      Returns:
      The default separator for this parser.
    • getQuotechar

      public char getQuotechar()
      Returns:
      The default quotation character for this parser.
    • getEscape

      public char getEscape()
      Returns:
      The default escape character for this parser.
    • isStrictQuotes

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

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

      public boolean isIgnoreQuotations()
      Returns:
      The default ignoreQuotation setting for this parser.
    • isPending

      public boolean isPending()
      Returns:
      True if something was left over from last call(s)
    • parseLine

      public List<String> parseLine(String nextLine) throws ParseException
      Parses an incoming String and returns an array of elements. This method is used when all data is contained in a single line.
      Parameters:
      nextLine - Line to be parsed.
      Returns:
      The comma-tokenized list of elements, or null if nextLine is null
      Throws:
      ParseException - If bad things happen during the read
    • parseLineMulti

      public List<String> parseLineMulti(String nextLine) throws ParseException
      Parses an incoming String and returns an array of elements. This method is used when the data spans multiple lines.
      Parameters:
      nextLine - Current line to be processed
      Returns:
      The comma-tokenized list of elements, or null if nextLine is null
      Throws:
      ParseException - If bad things happen during the read
    • parseLineToArray

      public String[] parseLineToArray(String nextLine) throws ParseException
      Throws:
      ParseException
    • parseLineMultiToArray

      public String[] parseLineMultiToArray(String nextLine) throws ParseException
      Throws:
      ParseException
    • parseLineToArray

      public String[] parseLineToArray(String[] output, String nextLine) throws ParseException
      Throws:
      ParseException
    • parseLineMultiToArray

      public String[] parseLineMultiToArray(String[] output, String nextLine) throws ParseException
      Throws:
      ParseException