Package org.apache.commons.csv
Class CSVFormat.Builder
- java.lang.Object
-
- org.apache.commons.csv.CSVFormat.Builder
-
- Enclosing class:
- CSVFormat
public static class CSVFormat.Builder extends java.lang.ObjectBuilds CSVFormat instances.- Since:
- 1.9.0
-
-
Field Summary
Fields Modifier and Type Field Description private booleanallowDuplicateHeaderNamesprivate booleanallowMissingColumnNamesprivate booleanautoFlushprivate java.lang.CharactercommentMarkerprivate java.lang.Stringdelimiterprivate java.lang.CharacterescapeCharacterprivate java.lang.String[]headerCommentsprivate java.lang.String[]headersprivate booleanignoreEmptyLinesprivate booleanignoreHeaderCaseprivate booleanignoreSurroundingSpacesprivate java.lang.StringnullStringprivate java.lang.CharacterquoteCharacterprivate java.lang.StringquotedNullStringprivate QuoteModequoteModeprivate java.lang.StringrecordSeparatorprivate booleanskipHeaderRecordprivate booleantrailingDelimiterprivate booleantrim
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CSVFormatbuild()Builds a new CSVFormat instance.static CSVFormat.Buildercreate()Creates a new default builder.static CSVFormat.Buildercreate(CSVFormat csvFormat)Creates a new builder for the given format.CSVFormat.BuildersetAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames)Sets the duplicate header names behavior, true to allow, false to disallow.CSVFormat.BuildersetAllowMissingColumnNames(boolean allowMissingColumnNames)Sets the missing column names behavior,trueto allow missing column names in the header line,falseto cause anIllegalArgumentExceptionto be thrown.CSVFormat.BuildersetAutoFlush(boolean autoFlush)Sets whether to flush on close.CSVFormat.BuildersetCommentMarker(char commentMarker)Sets the comment start marker, usenullto disable.CSVFormat.BuildersetCommentMarker(java.lang.Character commentMarker)Sets the comment start marker, usenullto disable.CSVFormat.BuildersetDelimiter(char delimiter)Sets the delimiter character.CSVFormat.BuildersetDelimiter(java.lang.String delimiter)Sets the delimiter character.CSVFormat.BuildersetEscape(char escapeCharacter)Sets the escape character.CSVFormat.BuildersetEscape(java.lang.Character escapeCharacter)Sets the escape character.CSVFormat.BuildersetHeader(java.lang.Class<? extends java.lang.Enum<?>> headerEnum)Sets the header defined by the givenEnumclass.CSVFormat.BuildersetHeader(java.lang.String... header)Sets the header to the given values.CSVFormat.BuildersetHeader(java.sql.ResultSet resultSet)Sets the header from the result set metadata.CSVFormat.BuildersetHeader(java.sql.ResultSetMetaData resultSetMetaData)Sets the header from the result set metadata.CSVFormat.BuildersetHeaderComments(java.lang.Object... headerComments)Sets the header comments set to the given values.CSVFormat.BuildersetHeaderComments(java.lang.String... headerComments)Sets the header comments set to the given values.CSVFormat.BuildersetIgnoreEmptyLines(boolean ignoreEmptyLines)Sets the empty line skipping behavior,trueto ignore the empty lines between the records,falseto translate empty lines to empty records.CSVFormat.BuildersetIgnoreHeaderCase(boolean ignoreHeaderCase)Sets the case mapping behavior,trueto access name/values,falseto leave the mapping as is.CSVFormat.BuildersetIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces)Sets the parser trimming behavior,trueto remove the surrounding spaces,falseto leave the spaces as is.CSVFormat.BuildersetNullString(java.lang.String nullString)Sets the String to convert to and fromnull.CSVFormat.BuildersetQuote(char quoteCharacter)Sets the quote character.CSVFormat.BuildersetQuote(java.lang.Character quoteCharacter)Sets the quote character, usenullto disable.CSVFormat.BuildersetQuoteMode(QuoteMode quoteMode)Sets the quote policy to use for output.CSVFormat.BuildersetRecordSeparator(char recordSeparator)Sets the record separator to use for output.CSVFormat.BuildersetRecordSeparator(java.lang.String recordSeparator)Sets the record separator to use for output.CSVFormat.BuildersetSkipHeaderRecord(boolean skipHeaderRecord)Sets whether to skip the header record.CSVFormat.BuildersetTrailingDelimiter(boolean trailingDelimiter)Sets whether to add a trailing delimiter.CSVFormat.BuildersetTrim(boolean trim)Sets whether to trim leading and trailing blanks.
-
-
-
Field Detail
-
allowDuplicateHeaderNames
private boolean allowDuplicateHeaderNames
-
allowMissingColumnNames
private boolean allowMissingColumnNames
-
autoFlush
private boolean autoFlush
-
commentMarker
private java.lang.Character commentMarker
-
delimiter
private java.lang.String delimiter
-
escapeCharacter
private java.lang.Character escapeCharacter
-
headerComments
private java.lang.String[] headerComments
-
headers
private java.lang.String[] headers
-
ignoreEmptyLines
private boolean ignoreEmptyLines
-
ignoreHeaderCase
private boolean ignoreHeaderCase
-
ignoreSurroundingSpaces
private boolean ignoreSurroundingSpaces
-
nullString
private java.lang.String nullString
-
quoteCharacter
private java.lang.Character quoteCharacter
-
quotedNullString
private java.lang.String quotedNullString
-
quoteMode
private QuoteMode quoteMode
-
recordSeparator
private java.lang.String recordSeparator
-
skipHeaderRecord
private boolean skipHeaderRecord
-
trailingDelimiter
private boolean trailingDelimiter
-
trim
private boolean trim
-
-
Constructor Detail
-
Builder
private Builder(CSVFormat csvFormat)
-
-
Method Detail
-
create
public static CSVFormat.Builder create()
Creates a new default builder.- Returns:
- a copy of the builder
-
create
public static CSVFormat.Builder create(CSVFormat csvFormat)
Creates a new builder for the given format.- Parameters:
csvFormat- the source format.- Returns:
- a copy of the builder
-
build
public CSVFormat build()
Builds a new CSVFormat instance.- Returns:
- a new CSVFormat instance.
-
setAllowDuplicateHeaderNames
public CSVFormat.Builder setAllowDuplicateHeaderNames(boolean allowDuplicateHeaderNames)
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
public CSVFormat.Builder setAllowMissingColumnNames(boolean allowMissingColumnNames)
Sets the 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
public CSVFormat.Builder setAutoFlush(boolean autoFlush)
Sets whether to flush on close.- Parameters:
autoFlush- whether to flush on close.- Returns:
- This instance.
-
setCommentMarker
public CSVFormat.Builder setCommentMarker(char commentMarker)
Sets the comment start marker, usenullto disable. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker- the comment start marker, usenullto disable.- Returns:
- This instance.
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
setCommentMarker
public CSVFormat.Builder setCommentMarker(java.lang.Character commentMarker)
Sets the comment start marker, usenullto disable. Note that the comment start character is only recognized at the start of a line.- Parameters:
commentMarker- the comment start marker, usenullto disable.- Returns:
- This instance.
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
setDelimiter
public CSVFormat.Builder setDelimiter(char delimiter)
Sets the delimiter character.- Parameters:
delimiter- the delimiter character.- Returns:
- This instance.
-
setDelimiter
public CSVFormat.Builder setDelimiter(java.lang.String delimiter)
Sets the delimiter character.- Parameters:
delimiter- the delimiter character.- Returns:
- This instance.
-
setEscape
public CSVFormat.Builder setEscape(char escapeCharacter)
Sets the escape character.- Parameters:
escapeCharacter- the escape character.- Returns:
- This instance.
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
setEscape
public CSVFormat.Builder setEscape(java.lang.Character escapeCharacter)
Sets the escape character.- Parameters:
escapeCharacter- the escape character.- Returns:
- This instance.
- Throws:
java.lang.IllegalArgumentException- thrown if the specified character is a line break
-
setHeader
public CSVFormat.Builder setHeader(java.lang.Class<? extends java.lang.Enum<?>> headerEnum)
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
public CSVFormat.Builder setHeader(java.sql.ResultSet resultSet) throws java.sql.SQLException
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,nullif disabled, empty if parsed automatically, user specified otherwise.- Returns:
- This instance.
- Throws:
java.sql.SQLException- SQLException if a database access error occurs or this method is called on a closed result set.
-
setHeader
public CSVFormat.Builder setHeader(java.sql.ResultSetMetaData resultSetMetaData) throws java.sql.SQLException
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,nullif disabled, empty if parsed automatically, user specified otherwise.- Returns:
- This instance.
- Throws:
java.sql.SQLException- SQLException if a database access error occurs or this method is called on a closed result set.
-
setHeader
public CSVFormat.Builder setHeader(java.lang.String... header)
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,nullif disabled, empty if parsed automatically, user specified otherwise.- Returns:
- This instance.
-
setHeaderComments
public CSVFormat.Builder setHeaderComments(java.lang.Object... headerComments)
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
public CSVFormat.Builder setHeaderComments(java.lang.String... headerComments)
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
public CSVFormat.Builder setIgnoreEmptyLines(boolean ignoreEmptyLines)
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
public CSVFormat.Builder setIgnoreHeaderCase(boolean ignoreHeaderCase)
Sets the 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
public CSVFormat.Builder setIgnoreSurroundingSpaces(boolean ignoreSurroundingSpaces)
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.
-
setNullString
public CSVFormat.Builder setNullString(java.lang.String nullString)
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
public CSVFormat.Builder setQuote(char quoteCharacter)
Sets the quote character.- Parameters:
quoteCharacter- the quote character.- Returns:
- This instance.
-
setQuote
public CSVFormat.Builder setQuote(java.lang.Character quoteCharacter)
Sets the quote character, usenullto disable.- Parameters:
quoteCharacter- the quote character, usenullto disable.- Returns:
- This instance.
-
setQuoteMode
public CSVFormat.Builder setQuoteMode(QuoteMode quoteMode)
Sets the quote policy to use for output.- Parameters:
quoteMode- the quote policy to use for output.- Returns:
- This instance.
-
setRecordSeparator
public CSVFormat.Builder setRecordSeparator(char recordSeparator)
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
public CSVFormat.Builder setRecordSeparator(java.lang.String recordSeparator)
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
public CSVFormat.Builder setSkipHeaderRecord(boolean skipHeaderRecord)
Sets whether to skip the header record.- Parameters:
skipHeaderRecord- whether to skip the header record.- Returns:
- This instance.
-
setTrailingDelimiter
public CSVFormat.Builder setTrailingDelimiter(boolean trailingDelimiter)
Sets whether to add a trailing delimiter.- Parameters:
trailingDelimiter- whether to add a trailing delimiter.- Returns:
- This instance.
-
setTrim
public CSVFormat.Builder setTrim(boolean trim)
Sets whether to trim leading and trailing blanks.- Parameters:
trim- whether to trim leading and trailing blanks.- Returns:
- This instance.
-
-