Package org.apache.commons.csv
Class CSVFormat.Builder
java.lang.Object
org.apache.commons.csv.CSVFormat.Builder
- Enclosing class:
- CSVFormat
Builds CSVFormat instances.
- Since:
- 1.9.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private Character
private String
private DuplicateHeaderMode
private Character
private String[]
private String[]
private boolean
private boolean
private boolean
private String
private Character
private String
private QuoteMode
private String
private boolean
private boolean
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()
Builds a new CSVFormat instance.static CSVFormat.Builder
create()
Creates a new default builder.static CSVFormat.Builder
Creates a new builder for the given format.setAllowDuplicateHeaderNames
(boolean allowDuplicateHeaderNames) Deprecated.setAllowMissingColumnNames
(boolean allowMissingColumnNames) Sets the parser missing column names behavior,true
to allow missing column names in the header line,false
to cause anIllegalArgumentException
to be thrown.setAutoFlush
(boolean autoFlush) Sets whether to flush on close.setCommentMarker
(char commentMarker) Sets the comment start marker, usenull
to disable.setCommentMarker
(Character commentMarker) Sets the comment start marker, usenull
to disable.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 givenEnum
class.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 set to the given values.setHeaderComments
(String... headerComments) Sets the header comments set to the given values.setIgnoreEmptyLines
(boolean ignoreEmptyLines) Sets the empty line skipping behavior,true
to ignore the empty lines between the records,false
to translate empty lines to empty records.setIgnoreHeaderCase
(boolean ignoreHeaderCase) Sets the parser case mapping behavior,true
to access name/values,false
to leave the mapping as is.setIgnoreSurroundingSpaces
(boolean ignoreSurroundingSpaces) Sets the parser trimming behavior,true
to remove the surrounding spaces,false
to leave the spaces as is.setNullString
(String nullString) Sets the String to convert to and fromnull
.setQuote
(char quoteCharacter) Sets the quote character.Sets the quote character, usenull
to 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.setTrailingDelimiter
(boolean trailingDelimiter) Sets whether to add a trailing delimiter.setTrim
(boolean trim) Sets whether to trim leading and trailing blanks.
-
Field Details
-
allowMissingColumnNames
private boolean allowMissingColumnNames -
autoFlush
private boolean autoFlush -
commentMarker
-
delimiter
-
duplicateHeaderMode
-
escapeCharacter
-
headerComments
-
headers
-
ignoreEmptyLines
private boolean ignoreEmptyLines -
ignoreHeaderCase
private boolean ignoreHeaderCase -
ignoreSurroundingSpaces
private boolean ignoreSurroundingSpaces -
nullString
-
quoteCharacter
-
quotedNullString
-
quoteMode
-
recordSeparator
-
skipHeaderRecord
private boolean skipHeaderRecord -
trailingDelimiter
private boolean trailingDelimiter -
trim
private boolean trim
-
-
Constructor Details
-
Builder
-
-
Method Details
-
create
Creates a new default builder.- Returns:
- a copy of the builder
-
create
Creates a new builder for the given format.- Parameters:
csvFormat
- the source format.- Returns:
- a copy of the builder
-
build
Builds a new CSVFormat instance.- Returns:
- 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,true
to allow missing column names in the header line,false
to cause anIllegalArgumentException
to be thrown.- Parameters:
allowMissingColumnNames
- the missing column names behavior,true
to allow missing column names in the header line,false
to cause anIllegalArgumentException
to 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 start marker, usenull
to disable. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker
- the comment start marker, usenull
to disable.- Returns:
- This instance.
- Throws:
IllegalArgumentException
- thrown if the specified character is a line break
-
setCommentMarker
Sets the comment start marker, usenull
to disable. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker
- the comment start marker, usenull
to 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 givenEnum
class.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,null
if disabled, empty if parsed automatically, user specified otherwise.- Returns:
- This instance.
-
setHeader
Sets the header from the result set metadata. The header can either 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,null
if 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 either 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,null
if 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 either 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
.- Parameters:
header
- the header,null
if disabled, empty if parsed automatically, user specified otherwise.- Returns:
- This instance.
-
setHeaderComments
Sets the header comments set to the given values. The comments will be printed first, before the headers. This setting is ignored by the parser.builder.setHeaderComments("Generated by Apache Commons CSV.", Instant.now());
- Parameters:
headerComments
- the headerComments which will be printed by the Printer before the actual CSV data.- Returns:
- This instance.
-
setHeaderComments
Sets the header comments set to the given values. The comments will be printed first, before the headers. This setting is ignored by the parser.Builder.setHeaderComments("Generated by Apache Commons CSV.", Instant.now());
- Parameters:
headerComments
- the headerComments which will be printed by the Printer before the actual CSV data.- Returns:
- This instance.
-
setIgnoreEmptyLines
Sets the empty line skipping behavior,true
to ignore the empty lines between the records,false
to translate empty lines to empty records.- Parameters:
ignoreEmptyLines
- the empty line skipping behavior,true
to ignore the empty lines between the records,false
to translate empty lines to empty records.- Returns:
- This instance.
-
setIgnoreHeaderCase
Sets the parser case mapping behavior,true
to access name/values,false
to leave the mapping as is.- Parameters:
ignoreHeaderCase
- the case mapping behavior,true
to access name/values,false
to leave the mapping as is.- Returns:
- This instance.
-
setIgnoreSurroundingSpaces
Sets the parser trimming behavior,true
to remove the surrounding spaces,false
to leave the spaces as is.- Parameters:
ignoreSurroundingSpaces
- the parser trimming behavior,true
to remove the surrounding spaces,false
to leave the spaces as is.- Returns:
- This instance.
-
setNullString
Sets the String to convert to and fromnull
. No substitution occurs ifnull
.- Reading: Converts strings equal to the given
nullString
tonull
when reading records. - Writing: Writes
null
as the givennullString
when 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, usenull
to disable.- Parameters:
quoteCharacter
- the quote character, usenull
to 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.
-
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.
-
setDuplicateHeaderMode(DuplicateHeaderMode)
.