You can use one of the predefined formats:
DEFAULTEXCELINFORMIX_UNLOADINFORMIX_UNLOAD_CSVMONGODB_CSVMONGODB_TSVMYSQLORACLEPOSTGRESQL_CSVPOSTGRESQL_TEXTRFC4180TDFFor example:
CSVParser parser = CSVFormat.EXCEL.parse(reader);
The CSVParser provides static methods to parse other input types, for example:
CSVParser parser = CSVParser.parse(file, StandardCharsets.US_ASCII, CSVFormat.EXCEL);
You can extend a format by calling the set methods. For example:
CSVFormat.EXCEL.builder().setNullString("N/A").setIgnoreSurroundingSpaces(true).get();
To define the column names you want to use to access records, write:
CSVFormat.EXCEL.builder().setHeader("Col1", "Col2", "Col3").get();
Calling Builder#setHeader(String. lets you use the given names to address values in a CSVRecord, and assumes that your CSV source does not
contain a first record that also defines column names.
If it does, then you are overriding this metadata with your names and you should skip the first record by calling
Builder#setSkipHeaderRecord(boolean) with true.
You can use a format directly to parse a reader. For example, to parse an Excel file with columns header, write:
Reader in = ...;
CSVFormat.EXCEL.builder().setHeader("Col1", "Col2", "Col3").get().parse(in);
For other input types, like resources, files, and URLs, use the static methods on CSVParser.
If your source contains a header record, you can simplify your code and safely reference columns, by using Builder#setHeader(String. with no
arguments:
CSVFormat.EXCEL.builder().setHeader().get();
This causes the parser to read the first record and use its values as column names.
Then, call one of the CSVRecord get method that takes a String column name argument:
String value = record.get("Col1");
This makes your code impervious to changes in column order in the CSV file.
This class implements the Serializable interface with the following caveats:
The serialVersionUID values are:
2L1LThis class is immutable.
Not all settings are used for both parsing and writing.
| Modifier and Type | Class and Description |
|---|---|
| public static class | CSVFormat.
Builds CSVFormat instances. |
| public static enum | CSVFormat.
Predefines formats. |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | allowMissingColumnNames
Whether missing column names are allowed when parsing the header line. |
| private final boolean | autoFlush
Whether to flush on close. |
| private final Character | commentMarker
Set to null if commenting is disabled. |
| public static final CSVFormat | |
| private final String | delimiter
The character delimiting the values (typically ";", "," or "\t"). |
| private final DuplicateHeaderMode | duplicateHeaderMode
How duplicate headers are handled. |
| private final Character | escapeCharacter
Set to null if escaping is disabled. |
| public static final CSVFormat | EXCEL
Microsoft Excel file format (using a comma as the value delimiter). |
| private final String[] | headerComments
Array of header comment lines. |
| private final String[] | headers
Array of header column names. |
| private final boolean | ignoreEmptyLines
Whether empty lines between records are ignored when parsing input. |
| private final boolean | ignoreHeaderCase
Should ignore header names case. |
| private final boolean | ignoreSurroundingSpaces
Should leading/trailing spaces be ignored around values?. |
| public static final CSVFormat | INFORMIX_UNLOAD
Default Informix CSV UNLOAD
format used by the |
| public static final CSVFormat | INFORMIX_UNLOAD_CSV
Default Informix CSV UNLOAD
format used by the This is a comma-delimited format with an LF character as the line separator. |
| private final boolean | lenientEof
Whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility. |
| private final long | maxRows
The maximum number of rows to process, excluding the header row. |
| public static final CSVFormat | MONGODB_CSV
Default MongoDB CSV format used by the |
| public static final CSVFormat | MONGODB_TSV
Default MongoDB TSV format used by the |
| public static final CSVFormat | |
| private final String | nullString
The string to be used for null values. |
| public static final CSVFormat | |
| public static final CSVFormat | POSTGRESQL_CSV
Default PostgreSQL CSV format used by the |
| public static final CSVFormat | POSTGRESQL_TEXT
Default PostgreSQL Text format used by the |
| private final Character | quoteCharacter
Set to null if quoting is disabled. |
| private final String | quotedNullString
Set to |
| private final QuoteMode | quoteMode
The quote policy output fields. |
| private final String | recordSeparator
For output. |
| public static final CSVFormat | |
| private static final long | |
| private final boolean | skipHeaderRecord
Whether to skip the header record. |
| public static final CSVFormat | |
| private final boolean | trailingData
Whether reading trailing data is allowed in records, helps Excel compatibility. |
| private final boolean | trailingDelimiter
Whether to add a trailing delimiter. |
| private final boolean | trim
Whether to trim leading and trailing blanks. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private void | |
| public CSVFormat. | |
| pack-priv static < The array element type. T> T[] | |
| private static boolean | |
| private static boolean | Returns: true ifc contains a line break character.the string to check. source)Returns true if the given string contains a line break character. |
| pack-priv static CSVFormat | |
| pack-priv CSVFormat | |
| public boolean | equals(final Object
the reference object with which to compare. obj)Overrides java. Indicates whether some other object is "equal to" this one. |
| private void | |
| public String | |
| private String | |
| public boolean | Returns: whether duplicate header names are allowed
Deprecated
Use
getDuplicateHeaderMode().
Gets whether duplicate names are allowed in the headers. |
| public boolean | Returns: true if missing column names are allowed when parsing the header line, false to throw an IllegalArgumentException.Gets whether missing column names are allowed when parsing the header line. |
| public boolean | |
| public Character | Returns: the comment start marker, may benullGets the comment marker character, |
| public char | Returns: the first delimiter character.
Deprecated
Use
getDelimiterString().
Gets the first character delimiting the values (typically ';', ',' or '\t'). |
| pack-priv char[] | Returns: the delimiter.Gets the character delimiting the values (typically ";", "," or "\t"). |
| public String | Returns: the delimiter.Gets the character delimiting the values (typically ";", "," or "\t"). |
| public DuplicateHeaderMode | Returns: if duplicate header values are allowed, allowed conditionally, or disallowed.Gets how duplicate headers are handled. |
| pack-priv char | |
| public Character | |
| public String[] | Returns: a copy of the header array;null if disabled, the empty array if to be read from the fileGets a copy of the header array. |
| public String[] | Returns: a copy of the header comment array;null if disabled.Gets a copy of the header comment array to write before the CSV data. |
| public boolean | Returns: true if empty lines between records are ignored, false if they are turned into empty records.Gets whether empty lines between records are ignored when parsing input. |
| public boolean | Returns: true if header names cases are ignored, false if they are case-sensitive.Gets whether header names will be accessed ignoring case when parsing input. |
| public boolean | Returns: true if spaces around values are ignored, false if they are treated as part of the value.Gets whether spaces around values are ignored when parsing input. |
| public boolean | Returns: whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility.Gets whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility. |
| public long | Returns: The maximum number of rows to process, excluding the header row.Gets the maximum number of rows to process, excluding the header row. |
| public String | Returns: the String to convert to and fromnull. No substitution occurs if nullGets the String to convert to and from |
| public Character | Returns: the quoteChar character, may benullGets the character used to encapsulate values containing special characters. |
| public QuoteMode | |
| public String | Returns: the record separatorGets the record separator delimiting output records. |
| public boolean | Returns: whether to skip the header record.Gets whether to skip the header record. |
| public boolean | Returns: whether reading trailing data is allowed in records, helps Excel compatibility.Gets whether reading trailing data is allowed in records, helps Excel compatibility. |
| public boolean | Returns: whether to add a trailing delimiter.Gets whether to add a trailing delimiter. |
| public boolean | Returns: whether to trim leading and trailing blanks.Gets whether to trim leading and trailing blanks. |
| public int | |
| pack-priv static boolean | |
| public boolean | Returns: true is comments are supported, false otherwiseTests whether comments are supported by this format. |
| private boolean | Returns: true if the match is successfulthe first char (index 0). ch0, final CharSequence the match char sequence charSeq, final int where start to match startIndex, final char[] the delimiter delimiter, final int the delimiter length delimiterLength)Tests whether the next characters constitute a delimiter |
| public boolean | Returns: true if escapes are processedTests whether escapes are being processed. |
| private static boolean | Returns: true ifc is a line break character.the character to check. c)Returns true if the given character is a line break character. |
| private static boolean | Returns: true ifc is a line break character (and not null).the character to check, may be null. c)Returns true if the given character is a line break character. |
| public boolean | Returns: true if a nullString is definedTests whether a null string has been defined. |
| public boolean | Returns: true if a quoteChar is definedTests whether a quoteChar has been defined. |
| private static boolean | |
| private static boolean | |
| pack-priv <T> IOStream | |
| public static CSVFormat | Returns: a new CSV format.the char used for value separation, must not be a line break character delimiter)Creates a new CSV format with the specified delimiter. |
| public CSVParser | |
| public CSVPrinter | Returns: a printer to an output.the output. out)Prints to the specified output. |
| public CSVPrinter | |
| private void | |
| public synchronized void | print(final Object
value to output. value, final Appendable where to print the value. out, final boolean if this a new record. newRecord)Prints the |
| private synchronized void | |
| public CSVPrinter | |
| private void | |
| public CSVPrinter | |
| public synchronized void | println(final Appendable
where to write appendable)Outputs the trailing delimiter (if set) followed by the record separator (if set). |
| public synchronized void | printRecord(final Appendable
where to write. appendable, final Object... values to output. values)Prints the given |
| private void | |
| private void | |
| private void | printWithQuotes(final Object object, final CharSequence charSeq, final Appendable out, final boolean newRecord)
|
| private void | printWithQuotes(final Reader
What to print reader, final Appendable Where to print it appendable)Always use quotes unless QuoteMode is NONE, so we do not have to look ahead. |
| public String | |
| pack-priv static String[] | |
| pack-priv static CharSequence | |
| pack-priv String | |
| pack-priv boolean | |
| pack-priv boolean | |
| private void | validate()
Verifies the validity and consistency of the attributes, and throws an |
| public static CSVFormat | Returns: one of the predefined formatsname format)Gets one of the predefined formats from |
| public CSVFormat | Returns: a newCSVFormat that allows duplicate header names
Deprecated
Use
Builder#setAllowDuplicateHeaderNames(true)
Builds a new |
| public CSVFormat | Returns: a newCSVFormat with duplicate header names behavior set to the given value.the duplicate header names behavior, true to allow, false to disallow. allowDuplicateHeaderNames)
Deprecated
Use
Builder#setAllowDuplicateHeaderNames(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified missing column names behavior.
Deprecated
Use
Builder#setAllowMissingColumnNames(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified missing column names behavior.the missing column names behavior, allowMissingColumnNames)true to allow missing column names in the header line, false to cause
an IllegalArgumentException to be thrown.
Deprecated
Use
Builder#setAllowMissingColumnNames(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified autoFlush setting.whether to flush on close. autoFlush)
Deprecated
Use
Builder#setAutoFlush(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this one but with the specified character as the comment start markerthe comment start marker commentMarker)
Deprecated
Use
Builder#setCommentMarker(char)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this one but with the specified character as the comment start markerthe comment start marker, use commentMarker)null to disable
Deprecated
Use
Builder#setCommentMarker(Character)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this with the specified character as a delimiterthe delimiter character delimiter)
Deprecated
Use
Builder#setDelimiter(char)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified character as the escape characterthe escape character escape)
Deprecated
Use
Builder#setEscape(char)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified character as the escape characterthe escape character, use escape)null to disable
Deprecated
Use
Builder#setEscape(Character)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but using the first record as header.
Deprecated
Use
Builder#setHeader().
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified headerthe enum defining the header, headerEnum)null if disabled, empty if parsed automatically, user specified otherwise.
Deprecated
Use
Builder#setHeader(Class)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified headerthe resultSet for the header, resultSet)null if disabled, empty if parsed automatically, user-specified otherwise.
Deprecated
Use
Builder#setHeader(ResultSet)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified headerthe metaData for the header, resultSetMetaData)null if disabled, empty if parsed automatically, user specified otherwise.
Deprecated
Use
Builder#setHeader(ResultSetMetaData)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified headerthe header, header)null if disabled, empty if parsed automatically, user-specified otherwise.
Deprecated
Use
Builder#setHeader(String.
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified headerthe headerComments which will be printed by the Printer before the actual CSV data. headerComments)
Deprecated
Use
Builder#setHeaderComments(Object.
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified empty line skipping behavior.
Deprecated
Use
Builder#setIgnoreEmptyLines(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified empty line skipping behavior.the empty line skipping behavior, ignoreEmptyLines)true to ignore the empty lines between the records, false to translate empty
lines to empty records.
Deprecated
Use
Builder#setIgnoreEmptyLines(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that will ignore the new case header name behavior.
Deprecated
Use
Builder#setIgnoreHeaderCase(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that will ignore case header name if specified astruethe case mapping behavior, ignoreHeaderCase)true to access name/values, false to leave the mapping as is.
Deprecated
Use
Builder#setIgnoreHeaderCase(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified parser trimming behavior.
Deprecated
Use
Builder#setIgnoreSurroundingSpaces(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified trimming behavior.the parser trimming behavior, ignoreSurroundingSpaces)true to remove the surrounding spaces, false to leave the spaces as is.
Deprecated
Use
Builder#setIgnoreSurroundingSpaces(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified null conversion string.the String to convert to and from nullString)null. No substitution occurs if null
Deprecated
Use
Builder#setNullString(String)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified character as quoteCharthe quote character quoteChar)
Deprecated
Use
Builder#setQuote(char)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified character as quoteCharthe quote character, use quoteChar)null to disable.
Deprecated
Use
Builder#setQuote(Character)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified quote policythe quote policy to use for output. quoteMode)
Deprecated
Use
Builder#setQuoteMode(QuoteMode)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified output record separatorthe record separator to use for output. recordSeparator)
Deprecated
Use
Builder#setRecordSeparator(char)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified output record separatorthe record separator to use for output. recordSeparator)
Deprecated
Use
Builder#setRecordSeparator(String)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting.
Deprecated
Use
Builder#setSkipHeaderRecord(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified skipHeaderRecord setting.whether to skip the header record. skipHeaderRecord)
Deprecated
Use
Builder#setSkipHeaderRecord(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the operating system's line separator string.
Deprecated
Use
setRecordSeparator(System.
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the trailing delimiter setting.
Deprecated
Use
Builder#setTrailingDelimiter(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified trailing delimiter setting.whether to add a trailing delimiter. trailingDelimiter)
Deprecated
Use
Builder#setTrailingDelimiter(boolean)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the trim setting on.
Deprecated
Use
Builder#setTrim(true)
Builds a new |
| public CSVFormat | Returns: A new CSVFormat that is equal to this but with the specified trim setting.whether to trim leading and trailing blanks. trim)
Deprecated
Use
Builder#setTrim(boolean)
Builds a new |