public class CSVParser extends AbstractCSVParser
A very simple CSV parser released under a commercial-friendly license. This just implements splitting a single line into fields.
The purpose of the CSVParser is to take a single string and parse it into its elements based on the delimiter, quote and escape characters.
The CSVParser has grown organically based on user requests and does not truly match any current requirements (though it can be configured to match or come close). There are no plans to change this as it will break existing requirements. Consider using the RFC4180Parser for all standard csv data. It may be less configurable, but that is not needed for data matching the RFC4180 requirements.
nullFieldIndicator, pending, quotechar, separatorDEFAULT_BUNDLE_NAME, DEFAULT_ESCAPE_CHARACTER, DEFAULT_IGNORE_LEADING_WHITESPACE, DEFAULT_IGNORE_QUOTATIONS, DEFAULT_NULL_FIELD_INDICATOR, DEFAULT_QUOTE_CHARACTER, DEFAULT_SEPARATOR, DEFAULT_STRICT_QUOTES, INITIAL_READ_SIZE, MAX_SIZE_FOR_EMPTY_FIELD, NEWLINE, NULL_CHARACTER, READ_BUFFER_SIZE| Modifier | Constructor and Description |
|---|---|
|
CSVParser()
Constructs CSVParser using default values for everything.
|
protected |
CSVParser(char separator,
char quotechar,
char escape,
boolean strictQuotes,
boolean ignoreLeadingWhiteSpace,
boolean ignoreQuotations,
CSVReaderNullFieldIndicator nullFieldIndicator,
Locale errorLocale)
Constructs CSVParser.
|
| Modifier and Type | Method and Description |
|---|---|
protected String |
convertToCsvValue(String value,
boolean applyQuotestoAll)
Used when reverse parsing an array of strings to a single string.
|
char |
getEscape() |
boolean |
isIgnoreLeadingWhiteSpace() |
boolean |
isIgnoreQuotations() |
protected boolean |
isNextCharacterEscapable(String nextLine,
boolean inQuotes,
int i)
Checks to see if the character after the current index in a String is an
escapable character.
|
boolean |
isStrictQuotes() |
protected String[] |
parseLine(String nextLine,
boolean multi)
Parses an incoming
String and returns an array of elements. |
void |
setErrorLocale(Locale errorLocale)
Sets the locale for all error messages.
|
convertToCsvValue, getPendingText, getQuotechar, getQuotecharAsString, getQuoteDoubledAsString, getQuoteMatcherPattern, getSeparator, getSeparatorAsString, isPending, isSurroundWithQuotes, nullFieldIndicator, parseLine, parseLineMulti, parseToLine, parseToLinepublic CSVParser()
protected CSVParser(char separator,
char quotechar,
char escape,
boolean strictQuotes,
boolean ignoreLeadingWhiteSpace,
boolean ignoreQuotations,
CSVReaderNullFieldIndicator nullFieldIndicator,
Locale errorLocale)
This constructor sets all necessary parameters for CSVParser, and intentionally has package access so only the builder can use it.
separator - The delimiter to use for separating entriesquotechar - The character to use for quoted elementsescape - The character to use for escaping a separator or quotestrictQuotes - If true, characters outside the quotes are ignoredignoreLeadingWhiteSpace - If true, white space in front of a quote in a field is ignoredignoreQuotations - If true, treat quotations like any other character.nullFieldIndicator - Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES,
BOTH, NEITHER (default)errorLocale - Locale for error messages.public char getEscape()
public boolean isStrictQuotes()
public boolean isIgnoreLeadingWhiteSpace()
public boolean isIgnoreQuotations()
protected String convertToCsvValue(String value, boolean applyQuotestoAll)
AbstractCSVParserconvertToCsvValue in class AbstractCSVParservalue - String to be convertedapplyQuotestoAll - All values should be surrounded with quotesprotected String[] parseLine(String nextLine, boolean multi) throws IOException
AbstractCSVParserString and returns an array of elements.parseLine in class AbstractCSVParsernextLine - The string to parsemulti - Whether it takes multiple lines to form a single recordnull if nextLine is nullIOException - If bad things happen during the readprotected boolean isNextCharacterEscapable(String nextLine, boolean inQuotes, int i)
Meaning the next character is a quotation character, the escape char, or the separator and you are inside quotes.
"Inside quotes" in this context is interpreted liberally. For instance, if quotes are not expected but we are inside a field, that still counts for the purposes of this method as being "in quotes".
Precondition: the current character is an escape.nextLine - The current lineinQuotes - True if the current context is quotedi - Current index in linepublic void setErrorLocale(Locale errorLocale)
ICSVParsererrorLocale - Locale for error messages. If null, the default locale
is used.Copyright © 2005–2025. All rights reserved.