Class CSVFormat.Builder
- Enclosing class:
CSVFormat
- Since:
- 1.9.0
-
Method Summary
Modifier and TypeMethodDescriptionbuild()Deprecated.static CSVFormat.Buildercreate()Creates a new default builder, as forCSVFormat.RFC4180but allowing empty lines.static CSVFormat.BuilderCreates a new builder from the given format.get()Builds a new CSVFormat instance.setAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames) Deprecated.setAllowMissingColumnNames(boolean allowMissingColumnNames) Sets the parser missing column names behavior,trueto allow missing column names in the header line,falseto cause anIllegalArgumentExceptionto be thrown.setAutoFlush(boolean autoFlush) Sets whether to flush on close.setCommentMarker(char commentMarker) Sets the comment marker character, usenullto disable comments.setCommentMarker(Character commentMarker) Sets the comment marker character, usenullto disable comments.setDelimiter(char delimiter) Sets the delimiter character.setDelimiter(String delimiter) Sets the delimiter character.setDuplicateHeaderMode(DuplicateHeaderMode duplicateHeaderMode) Sets the duplicate header names behavior.setEscape(char escapeCharacter) Sets the escape character.Sets the escape character.Sets the header defined by the givenEnumclass.Sets the header to the given values.Sets the header from the result set metadata.setHeader(ResultSetMetaData resultSetMetaData) Sets the header from the result set metadata.setHeaderComments(Object... headerComments) Sets the header comments to write before the CSV data.setHeaderComments(String... headerComments) Sets the header comments to write before the CSV data.setIgnoreEmptyLines(boolean ignoreEmptyLines) Sets the empty line skipping behavior,trueto ignore the empty lines between the records,falseto translate empty lines to empty records.setIgnoreHeaderCase(boolean ignoreHeaderCase) Sets the parser case mapping behavior,trueto access name/values,falseto leave the mapping as is.setIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces) Sets the parser trimming behavior,trueto remove the surrounding spaces,falseto leave the spaces as is.setLenientEof(boolean lenientEof) Sets whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility.setMaxRows(long maxRows) Sets the maximum number of rows to process, excluding the header row.setNullString(String nullString) Sets the String to convert to and fromnull.setQuote(char quoteCharacter) Sets the quote character.Sets the quote character, usenullto disable.setQuoteMode(QuoteMode quoteMode) Sets the quote policy to use for output.setRecordSeparator(char recordSeparator) Sets the record separator to use for output.setRecordSeparator(String recordSeparator) Sets the record separator to use for output.setSkipHeaderRecord(boolean skipHeaderRecord) Sets whether to skip the header record.setTrailingData(boolean trailingData) Sets whether reading trailing data is allowed in records, helps Excel compatibility.setTrailingDelimiter(boolean trailingDelimiter) Sets whether to add a trailing delimiter.setTrim(boolean trim) Sets whether to trim leading and trailing blanks.
-
Method Details
-
create
Creates a new default builder, as forCSVFormat.RFC4180but allowing empty lines.The
CSVFormat.Buildersettings are:setDelimiter(',')setQuote('"')setRecordSeparator("\r\n")setIgnoreEmptyLines(true)setDuplicateHeaderMode(DuplicateHeaderMode.ALLOW_ALL)- All other values take their Java defaults,
falsefor booleans,nullfor object references.
- Returns:
- a copy of the builder
- See Also:
-
create
Creates a new builder from the given format.- Parameters:
csvFormat- the source format.- Returns:
- a new builder.
-
build
Deprecated.Useget().Builds a new CSVFormat instance.- Returns:
- a new CSVFormat instance.
-
get
Builds a new CSVFormat instance. -
setAllowDuplicateHeaderNames
@Deprecated public CSVFormat.Builder setAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames) Deprecated.Sets the duplicate header names behavior, true to allow, false to disallow.- Parameters:
allowDuplicateHeaderNames- the duplicate header names behavior, true to allow, false to disallow.- Returns:
- This instance.
-
setAllowMissingColumnNames
Sets the parser missing column names behavior,trueto allow missing column names in the header line,falseto cause anIllegalArgumentExceptionto be thrown.- Parameters:
allowMissingColumnNames- the missing column names behavior,trueto allow missing column names in the header line,falseto cause anIllegalArgumentExceptionto be thrown.- Returns:
- This instance.
-
setAutoFlush
Sets whether to flush on close.- Parameters:
autoFlush- whether to flush on close.- Returns:
- This instance.
-
setCommentMarker
Sets the comment marker character, usenullto disable comments.The comment start character is only recognized at the start of a line.
Comments are printed first, before headers.
Use
setCommentMarker(char)orsetCommentMarker(Character)to set the comment marker written at the start of each comment line.If the comment marker is not set, then the header comments are ignored.
For example:
builder.setCommentMarker('#').setHeaderComments("Generated by Apache Commons CSV", Instant.ofEpochMilli(0));writes:
# Generated by Apache Commons CSV. # 1970-01-01T00:00:00Z
- Parameters:
commentMarker- the comment start marker, usenullto disable.- Returns:
- This instance.
- Throws:
IllegalArgumentException- thrown if the specified character is a line break
-
setCommentMarker
Sets the comment marker character, usenullto disable comments.The comment start character is only recognized at the start of a line.
Comments are printed first, before headers.
Use
setCommentMarker(char)orsetCommentMarker(Character)to set the comment marker written at the start of each comment line.If the comment marker is not set, then the header comments are ignored.
For example:
builder.setCommentMarker('#').setHeaderComments("Generated by Apache Commons CSV", Instant.ofEpochMilli(0));writes:
# Generated by Apache Commons CSV. # 1970-01-01T00:00:00Z
- Parameters:
commentMarker- the comment start marker, usenullto disable.- Returns:
- This instance.
- Throws:
IllegalArgumentException- thrown if the specified character is a line break
-
setDelimiter
Sets the delimiter character.- Parameters:
delimiter- the delimiter character.- Returns:
- This instance.
-
setDelimiter
Sets the delimiter character.- Parameters:
delimiter- the delimiter character.- Returns:
- This instance.
-
setDuplicateHeaderMode
Sets the duplicate header names behavior.- Parameters:
duplicateHeaderMode- the duplicate header names behavior- Returns:
- This instance.
- Since:
- 1.10.0
-
setEscape
Sets the escape character.- Parameters:
escapeCharacter- the escape character.- Returns:
- This instance.
- Throws:
IllegalArgumentException- thrown if the specified character is a line break
-
setEscape
Sets the escape character.- Parameters:
escapeCharacter- the escape character.- Returns:
- This instance.
- Throws:
IllegalArgumentException- thrown if the specified character is a line break
-
setHeader
Sets the header defined by the givenEnumclass.Example:
public enum HeaderEnum { Name, Email, Phone } Builder builder = builder.setHeader(HeaderEnum.class);The header is also used by the
CSVPrinter.- Parameters:
headerEnum- the enum defining the header,nullif disabled, empty if parsed automatically, user-specified otherwise.- Returns:
- This instance.
-
setHeader
Sets the header from the result set metadata. The header can be parsed automatically from the input file with:builder.setHeader();
or specified manually with:builder.setHeader(resultSet);
The header is also used by the
CSVPrinter.- Parameters:
resultSet- the resultSet for the header,nullif disabled, empty if parsed automatically, user-specified otherwise.- Returns:
- This instance.
- Throws:
SQLException- SQLException if a database access error occurs or this method is called on a closed result set.
-
setHeader
Sets the header from the result set metadata. The header can be parsed automatically from the input file with:builder.setHeader();
or specified manually with:builder.setHeader(resultSetMetaData);
The header is also used by the
CSVPrinter.- Parameters:
resultSetMetaData- the metaData for the header,nullif disabled, empty if parsed automatically, user-specified otherwise.- Returns:
- This instance.
- Throws:
SQLException- SQLException if a database access error occurs or this method is called on a closed result set.
-
setHeader
Sets the header to the given values. The header can be parsed automatically from the input file with:builder.setHeader();
or specified manually with:builder.setHeader("name", "email", "phone");The header is also used by the
CSVPrinter.This method keeps a copy of the input array.
- Parameters:
header- the header,nullif disabled, empty if parsed automatically, user-specified otherwise.- Returns:
- This instance.
-
setHeaderComments
Sets the header comments to write before the CSV data.This setting is ignored by the parser.
Comments are printed first, before headers.
Use
setCommentMarker(char)orsetCommentMarker(Character)to set the comment marker written at the start of each comment line.If the comment marker is not set, then the header comments are ignored.
For example:
builder.setCommentMarker('#').setHeaderComments("Generated by Apache Commons CSV", Instant.ofEpochMilli(0));writes:
# Generated by Apache Commons CSV. # 1970-01-01T00:00:00Z
This method keeps a copy of the input array.
- Parameters:
headerComments- the headerComments which will be printed by the Printer before the CSV data.- Returns:
- This instance.
-
setHeaderComments
Sets the header comments to write before the CSV data.This setting is ignored by the parser.
Comments are printed first, before headers.
Use
setCommentMarker(char)orsetCommentMarker(Character)to set the comment marker written at the start of each comment line.If the comment marker is not set, then the header comments are ignored.
For example:
builder.setCommentMarker('#').setHeaderComments("Generated by Apache Commons CSV", Instant.ofEpochMilli(0).toString());writes:
# Generated by Apache Commons CSV. # 1970-01-01T00:00:00Z
This method keeps a copy of the input array.
- Parameters:
headerComments- the headerComments which will be printed by the Printer before the CSV data.- Returns:
- This instance.
-
setIgnoreEmptyLines
Sets the empty line skipping behavior,trueto ignore the empty lines between the records,falseto translate empty lines to empty records.- Parameters:
ignoreEmptyLines- the empty line skipping behavior,trueto ignore the empty lines between the records,falseto translate empty lines to empty records.- Returns:
- This instance.
-
setIgnoreHeaderCase
Sets the parser case mapping behavior,trueto access name/values,falseto leave the mapping as is.- Parameters:
ignoreHeaderCase- the case mapping behavior,trueto access name/values,falseto leave the mapping as is.- Returns:
- This instance.
-
setIgnoreSurroundingSpaces
Sets the parser trimming behavior,trueto remove the surrounding spaces,falseto leave the spaces as is.- Parameters:
ignoreSurroundingSpaces- the parser trimming behavior,trueto remove the surrounding spaces,falseto leave the spaces as is.- Returns:
- This instance.
-
setLenientEof
Sets whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility.- Parameters:
lenientEof- whether reading end-of-file is allowed even when input is malformed, helps Excel compatibility.- Returns:
- This instance.
- Since:
- 1.11.0
-
setMaxRows
Sets the maximum number of rows to process, excluding the header row.Values less than or equal to 0 mean no limit.
- Parameters:
maxRows- the maximum number of rows to process, excluding the header row.- Returns:
- This instance.
- Since:
- 1.14.0
-
setNullString
Sets the String to convert to and fromnull. No substitution occurs ifnull.- Reading: Converts strings equal to the given
nullStringtonullwhen reading records. - Writing: Writes
nullas the givennullStringwhen writing records.
- Parameters:
nullString- the String to convert to and fromnull. No substitution occurs ifnull.- Returns:
- This instance.
- Reading: Converts strings equal to the given
-
setQuote
Sets the quote character.- Parameters:
quoteCharacter- the quote character.- Returns:
- This instance.
-
setQuote
Sets the quote character, usenullto disable.- Parameters:
quoteCharacter- the quote character, usenullto disable.- Returns:
- This instance.
-
setQuoteMode
Sets the quote policy to use for output.- Parameters:
quoteMode- the quote policy to use for output.- Returns:
- This instance.
-
setRecordSeparator
Sets the record separator to use for output.Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"
- Parameters:
recordSeparator- the record separator to use for output.- Returns:
- This instance.
-
setRecordSeparator
Sets the record separator to use for output.Note: This setting is only used during printing and does not affect parsing. Parsing currently only works for inputs with '\n', '\r' and "\r\n"
- Parameters:
recordSeparator- the record separator to use for output.- Returns:
- This instance.
-
setSkipHeaderRecord
Sets whether to skip the header record.- Parameters:
skipHeaderRecord- whether to skip the header record.- Returns:
- This instance.
-
setTrailingData
Sets whether reading trailing data is allowed in records, helps Excel compatibility.- Parameters:
trailingData- whether reading trailing data is allowed in records, helps Excel compatibility.- Returns:
- This instance.
- Since:
- 1.11.0
-
setTrailingDelimiter
Sets whether to add a trailing delimiter.- Parameters:
trailingDelimiter- whether to add a trailing delimiter.- Returns:
- This instance.
-
setTrim
Sets whether to trim leading and trailing blanks.- Parameters:
trim- whether to trim leading and trailing blanks.- Returns:
- This instance.
-
get().