Class CsvWriterSettings
- All Implemented Interfaces:
Cloneable
CsvWriter
)
In addition to the configuration options provided by CommonWriterSettings
, the CsvWriterSettings include:
- emptyValue (defaults to null): Defines a replacement string to signify an empty value (which is not a null value)
If the writer has an empty String to write to the output, the emptyValue is used instead of an empty string
- quoteAllFields (defaults to false): By default, only values that contain a field separator are enclosed within quotes.
If this property is set to true, this indicates that all written values should be enclosed within quotes (as defined in
CsvFormat
)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
private boolean
private boolean
private char[]
private boolean
private FieldSelector
private boolean
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
addConfiguration
(Map<String, Object> out) final CsvWriterSettings
clone()
Clones this configuration object.final CsvWriterSettings
clone
(boolean clearInputSpecificSettings) Clones this configuration object to reuse user-provided settings.protected CsvFormat
Returns the default CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.char[]
Returns the list of characters that when present in a value to be written, will force the output value to be enclosed in quotes.boolean
Indicates that all written values should be enclosed within quotes (as defined inCsvFormat
)(package private) final FieldSelector
Returns the current selection of quoted fields (if any)boolean
Flag whether to quotenull
values sent to theCsvWriter
when the corresponding column is configured to be quoted viaquoteFields(String...)
, orgetQuoteAllFields()
evaluates totrue
.boolean
Indicates whether escape sequences should be written in unquoted values.final boolean
Indicates that the user will provide escaped input, and the writer will not try to introduce escape sequences.boolean
Flag indicating whether the writer should replace the the normalized line separator character specified inFormat.getNormalizedNewline()
by the sequence specified inFormat.getLineSeparator()
, when the value is enclosed within quotes.boolean
isQuotationTrigger
(char ch) Queries if a given character is a quotation trigger, i.e.boolean
Indicates whether the CSV writer should escape values that contain the quote character, by enclosing the entire value in quotes.quoteFields
(Enum... columns) Selects fields whose values should always be written within quotesquoteFields
(String... columns) Selects fields whose values should always be written within quotesquoteIndexes
(Integer... columns) Selects field positions whose values should always be written within quotesvoid
setEscapeUnquotedValues
(boolean escapeUnquotedValues) Configures the writer to process escape sequences in unquoted values.private <T> FieldSet<T>
setFieldSet
(FieldSet<T> fieldSet, T... values) Replaces the current quoted field selectionfinal void
setInputEscaped
(boolean isInputEscaped) Configures the writer to prevent it to introduce escape sequences.void
setNormalizeLineEndingsWithinQuotes
(boolean normalizeLineEndingsWithinQuotes) Flag indicating whether the writer should replace the the normalized line separator character specified inFormat.getNormalizedNewline()
by the sequence specified inFormat.getLineSeparator()
, when the value is enclosed within quotes.void
setQuotationTriggers
(char... quotationTriggers) Defines one or more "triggers" for enclosing a value within quotes.void
setQuoteAllFields
(boolean quoteAllFields) Indicates indicates whether or not all written values should be enclosed within quotes (as defined inCsvFormat
)void
setQuoteEscapingEnabled
(boolean quoteEscapingEnabled) Configures the CSV writer to escape values that contain the quote character, by enclosing the entire value in quotes.void
setQuoteNulls
(boolean quoteNulls) Configures whether to quotenull
values sent to theCsvWriter
when the corresponding column is configured to be quoted viaquoteFields(String...)
, orgetQuoteAllFields()
evaluates totrue
.Methods inherited from class com.univocity.parsers.common.CommonWriterSettings
clearInputSpecificSettings, configureFromAnnotations, getEmptyValue, getExpandIncompleteRows, getRowWriterProcessor, isColumnReorderingEnabled, isHeaderWritingEnabled, setColumnReorderingEnabled, setEmptyValue, setExpandIncompleteRows, setHeaderWritingEnabled, setRowWriterProcessor
Methods inherited from class com.univocity.parsers.common.CommonSettings
excludeFields, excludeFields, excludeIndexes, getErrorContentLength, getFormat, getHeaders, getIgnoreLeadingWhitespaces, getIgnoreTrailingWhitespaces, getMaxCharsPerColumn, getMaxColumns, getNullValue, getProcessorErrorHandler, getRowProcessorErrorHandler, getSkipBitsAsWhitespace, getSkipEmptyLines, getWhitespaceRangeStart, isAutoConfigurationEnabled, isProcessorErrorHandlerDefined, selectFields, selectFields, selectIndexes, setAutoConfigurationEnabled, setErrorContentLength, setFormat, setHeaders, setIgnoreLeadingWhitespaces, setIgnoreTrailingWhitespaces, setMaxCharsPerColumn, setMaxColumns, setNullValue, setProcessorErrorHandler, setRowProcessorErrorHandler, setSkipBitsAsWhitespace, setSkipEmptyLines, toString, trimValues
-
Field Details
-
escapeUnquotedValues
private boolean escapeUnquotedValues -
quoteAllFields
private boolean quoteAllFields -
isInputEscaped
private boolean isInputEscaped -
normalizeLineEndingsWithinQuotes
private boolean normalizeLineEndingsWithinQuotes -
quotationTriggers
private char[] quotationTriggers -
quoteEscapingEnabled
private boolean quoteEscapingEnabled -
quoteNulls
private boolean quoteNulls -
quotedFieldSelector
-
-
Constructor Details
-
CsvWriterSettings
public CsvWriterSettings()
-
-
Method Details
-
getQuoteAllFields
public boolean getQuoteAllFields()Indicates that all written values should be enclosed within quotes (as defined inCsvFormat
)(Defaults to false)
- Returns:
- true if all written values should be enclosed within quotes, false otherwise
-
setQuoteAllFields
public void setQuoteAllFields(boolean quoteAllFields) Indicates indicates whether or not all written values should be enclosed within quotes (as defined inCsvFormat
)(Defaults to false)
By default, only values that contain a field separator are enclosed within quotes.
- Parameters:
quoteAllFields
- a flag indicating whether to enclose all fields within quotes
-
isEscapeUnquotedValues
public boolean isEscapeUnquotedValues()Indicates whether escape sequences should be written in unquoted values. Defaults tofalse
.By default, this is disabled and if the input is
A""B,C
, the resulting value will be[A""B] and [C]
(i.e. the content is written as-is). However, if the writer is configured to process escape sequences in unquoted values, the values will be written as[A""""B] and [C]
- Returns:
- true if escape sequences should be processed in unquoted values, otherwise false
-
setEscapeUnquotedValues
public void setEscapeUnquotedValues(boolean escapeUnquotedValues) Configures the writer to process escape sequences in unquoted values. Defaults tofalse
.By default, this is disabled and if the input is
A""B,C
, the result will be written as[A""B] and [C]
(i.e. the quotes written as-is). However, if the writer is configured to process escape sequences in unquoted values, the values will written as[A""""B] and [C]
- Parameters:
escapeUnquotedValues
- a flag indicating whether escape sequences should be processed in unquoted values
-
isInputEscaped
public final boolean isInputEscaped()Indicates that the user will provide escaped input, and the writer will not try to introduce escape sequences. The input will be written as-is.Warning! ensure your data is properly escaped, otherwise the writer will produce invalid CSV.
This is disabled by default
- Returns:
- a flag indicating whether the escape sequences should not be introduced by the writer.
-
setInputEscaped
public final void setInputEscaped(boolean isInputEscaped) Configures the writer to prevent it to introduce escape sequences. The writer will assume the user is providing escaped input, and it will be written as-is.Warning! ensure your data is properly escaped, otherwise the writer will produce invalid CSV.
This is disabled by default
- Parameters:
isInputEscaped
- a flag indicating whether the input that will be written is already properly escaped.
-
isNormalizeLineEndingsWithinQuotes
public boolean isNormalizeLineEndingsWithinQuotes()Flag indicating whether the writer should replace the the normalized line separator character specified inFormat.getNormalizedNewline()
by the sequence specified inFormat.getLineSeparator()
, when the value is enclosed within quotes. This is enabled by default and is used to ensure data be read on any platform without introducing unwanted blank lines. For example, consider the quoted value"Line1 \n Line2"
. If this is written using"\r\n"
as the line separator sequence, and the normalized new line is set to'\n'
(the default), the output will be:[Line1 \r\n Line2]
However, if the value is meant to be kept untouched, and the original line separator should be maintained, set thenormalizeLineEndingsWithinQuotes
tofalse
. This will make the writer output the value as-is, producing:[Line1 \n Line2]
- Returns:
true
if line separator characters in quoted values should be considered 'normalized' and replaced by the sequence specified inFormat.getLineSeparator()
,false
otherwise
-
setNormalizeLineEndingsWithinQuotes
public void setNormalizeLineEndingsWithinQuotes(boolean normalizeLineEndingsWithinQuotes) Flag indicating whether the writer should replace the the normalized line separator character specified inFormat.getNormalizedNewline()
by the sequence specified inFormat.getLineSeparator()
, when the value is enclosed within quotes. This is enabled by default and is used to ensure data can be used on any platform without producing unrecognized line endings. For example, consider the quoted value"Line1 \n Line2"
. If this is written using"\r\n"
as the line separator sequence, and the normalized new line is set to'\n'
(the default), the output will be:[Line1 \r\n Line2]
However, if the value is meant to be kept untouched, and the original line separator should be maintained, set thenormalizeLineEndingsWithinQuotes
tofalse
. This will make the writer output the value as-is, producing:[Line1 \n Line2]
- Parameters:
normalizeLineEndingsWithinQuotes
- flag indicating that line separator characters in quoted values should be considered 'normalized' and occurrences ofFormat.getNormalizedNewline()
should be replaced by the sequence specified inFormat.getLineSeparator()
-
createDefaultFormat
Returns the default CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.- Specified by:
createDefaultFormat
in classCommonSettings<CsvFormat>
- Returns:
- and instance of CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.
-
getQuotationTriggers
public char[] getQuotationTriggers()Returns the list of characters that when present in a value to be written, will force the output value to be enclosed in quotes.- Returns:
- the characters that will trigger values to be quoted when present in a value to be written.
-
setQuotationTriggers
public void setQuotationTriggers(char... quotationTriggers) Defines one or more "triggers" for enclosing a value within quotes. If one of the characters in the quotation trigger list is found in a value to be written, the entire value will be enclosed in quotes.- Parameters:
quotationTriggers
- a list of characters that when present in a value to be written, will force the output value to be enclosed in quotes.
-
isQuotationTrigger
public boolean isQuotationTrigger(char ch) Queries if a given character is a quotation trigger, i.e. a character that if present in a value to be written, will make the CSV writer enclose the entire value within quotes.- Parameters:
ch
- the character to be tested- Returns:
true
if the given character is a quotation trigger,false
otherwise.
-
isQuoteEscapingEnabled
public boolean isQuoteEscapingEnabled()Indicates whether the CSV writer should escape values that contain the quote character, by enclosing the entire value in quotes. For example, consider a value such as[My "precious" value]
. When quote escaping is enabled, the output will be:["My ""precious"" value"]
If disabled (the default), the value will be written as-is. Note that the CSV output will not conform to the RFC 4180 standard, but it will still be valid as the value does not contain line separators nor the delimiter character.- Returns:
- a flag indicating whether values containing quotes should be enclosed in quotes.
-
setQuoteEscapingEnabled
public void setQuoteEscapingEnabled(boolean quoteEscapingEnabled) Configures the CSV writer to escape values that contain the quote character, by enclosing the entire value in quotes. For example, consider a value such as[My "precious" value]
. When quote escaping is enabled, the output will be:["My ""precious"" value"]
If disabled (the default), the value will be written as-is. Note that the CSV output will not conform to the RFC 4180 standard, but it will still be valid as the value does not contain line separators nor the delimiter character.- Parameters:
quoteEscapingEnabled
- a flag indicating whether values containing quotes should be enclosed in quotes.
-
addConfiguration
- Overrides:
addConfiguration
in classCommonWriterSettings<CsvFormat>
-
clone
Description copied from class:CommonSettings
Clones this configuration object. Use alternativeCommonSettings.clone(boolean)
method to reset properties that are specific to a given input, such as header names and selection of fields.- Overrides:
clone
in classCommonWriterSettings<CsvFormat>
- Returns:
- a copy of all configurations applied to the current instance.
-
clone
Description copied from class:CommonSettings
Clones this configuration object to reuse user-provided settings. Properties that are specific to a given input (such as header names and selection of fields) can be reset to their defaults if theclearInputSpecificSettings
flag is set totrue
- Overrides:
clone
in classCommonWriterSettings<CsvFormat>
- Parameters:
clearInputSpecificSettings
- flag indicating whether to clear settings that are likely to be associated with a given input.- Returns:
- a copy of the configurations applied to the current instance.
-
getQuotedFieldSelector
Returns the current selection of quoted fields (if any)- Returns:
- the current selection of quoted fields
-
setFieldSet
Replaces the current quoted field selection- Parameters:
fieldSet
- the new set of selected fieldsvalues
- the values to include to the selection- Returns:
- the set of selected fields given in as a parameter.
-
quoteFields
Selects fields whose values should always be written within quotes- Parameters:
columns
- a selection of columns that will always be quoted- Returns:
- the (modifiable) set of selected fields to be quoted.
-
quoteFields
Selects fields whose values should always be written within quotes- Parameters:
columns
- a selection of columns that will always be quoted- Returns:
- the (modifiable) set of selected fields to be quoted.
-
quoteIndexes
Selects field positions whose values should always be written within quotes- Parameters:
columns
- a selection of column indexes that will always be quoted- Returns:
- the (modifiable) set of column positions to be quoted.
-
setQuoteNulls
public void setQuoteNulls(boolean quoteNulls) Configures whether to quotenull
values sent to theCsvWriter
when the corresponding column is configured to be quoted viaquoteFields(String...)
, orgetQuoteAllFields()
evaluates totrue
.- Parameters:
quoteNulls
- flag indicating whethernull
values should be quoted.
-
getQuoteNulls
public boolean getQuoteNulls()Flag whether to quotenull
values sent to theCsvWriter
when the corresponding column is configured to be quoted viaquoteFields(String...)
, orgetQuoteAllFields()
evaluates totrue
.- Returns:
- a flag indicating whether
null
values should be quoted.
-