Package com.univocity.parsers.csv
Class CsvWriter
- java.lang.Object
-
- com.univocity.parsers.common.AbstractWriter<CsvWriterSettings>
-
- com.univocity.parsers.csv.CsvWriter
-
public class CsvWriter extends AbstractWriter<CsvWriterSettings>
A powerful and flexible CSV writer implementation.- See Also:
CsvFormat
,CsvWriterSettings
,CsvParser
,AbstractWriter
-
-
Field Summary
Fields Modifier and Type Field Description private char
delimiter
private boolean
dontProcessNormalizedNewLines
private char
escapeChar
private char
escapeEscape
private boolean
escapeUnquoted
private boolean
inputNotEscaped
private char
maxTrigger
private char[]
multiDelimiter
private char
newLine
private boolean[]
quotationTriggers
private boolean
quoteAllFields
private char
quoteChar
private java.util.Set<java.lang.Integer>
quotedColumns
private FieldSelector
quotedFieldSelector
private boolean
quoteNulls
-
Fields inherited from class com.univocity.parsers.common.AbstractWriter
appender, comment, emptyValue, expandRows, headers, headerTrimFlags, ignoreLeading, ignoreTrailing, nullValue, recordCount, usingNullOrEmptyValue, whitespaceRangeStart, writingHeaders
-
-
Constructor Summary
Constructors Constructor Description CsvWriter(CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.File file, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.File file, java.lang.String encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.File file, java.nio.charset.Charset encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.OutputStream output, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.OutputStream output, java.lang.String encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.OutputStream output, java.nio.charset.Charset encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.CsvWriter(java.io.Writer writer, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
append(int columnIndex, boolean isElementQuoted, boolean allowTrim, java.lang.String element)
private void
appendQuoted(int start, boolean allowTrim, java.lang.String element)
protected void
initialize(CsvWriterSettings settings)
Initializes the CSV writer with CSV-specific configurationprivate boolean
matchMultiDelimiter(java.lang.String element, int from)
protected void
processRow(java.lang.Object[] row)
Format-specific implementation for writing a single record into the output.private boolean
quoteElement(int start, java.lang.String element)
-
Methods inherited from class com.univocity.parsers.common.AbstractWriter
addStringValues, addValue, addValue, addValue, addValues, addValues, allowTrim, appendToRow, appendToRow, appendValueToRow, close, commentRow, commentRowToString, discardValues, enableNewlineAfterRecord, expand, flush, getRecordCount, getStringValue, processObjectRecords, processObjectRecordsAndClose, processObjectRecordsAndClose, processObjectRecordsToString, processObjectRecordsToString, processRecord, processRecord, processRecord, processRecord, processRecord, processRecords, processRecords, processRecords, processRecords, processRecords, processRecordsAndClose, processRecordsAndClose, processRecordsAndClose, processRecordsAndClose, processRecordsToString, processRecordsToString, processRecordsToString, processRecordsToString, processRecordToString, processRecordToString, processRecordToString, processRecordToString, processRecordToString, processValuesToRow, processValuesToString, skipLeadingWhitespace, updateFieldExclusion, updateFieldExclusion, updateFieldSelection, updateFieldSelection, writeEmptyRow, writeHeaders, writeHeaders, writeHeaders, writeHeadersToString, writeHeadersToString, writeHeadersToString, writeObjectRows, writeObjectRowsAndClose, writeObjectRowsAndClose, writeObjectRowsToString, writeRecord, writeRecords, writeRecords, writeRecordsAndClose, writeRecordsAndClose, writeRecordsAndClose, writeRecordsToString, writeRecordsToString, writeRecordToString, writeRow, writeRow, writeRow, writeRow, writeRow, writeRow, writeRows, writeRows, writeRows, writeRows, writeRows, writeRowsAndClose, writeRowsAndClose, writeRowsAndClose, writeRowsAndClose, writeRowsAndClose, writeRowsToString, writeRowsToString, writeRowsToString, writeRowsToString, writeRowsToString, writeRowToString, writeRowToString, writeRowToString, writeRowToString, writeRowToString, writeStringRows, writeStringRows, writeStringRows, writeStringRowsAndClose, writeStringRowsAndClose, writeStringRowsAndClose, writeStringRowsToString, writeStringRowsToString, writeStringRowsToString, writeValuesToRow, writeValuesToString
-
-
-
-
Field Detail
-
delimiter
private char delimiter
-
multiDelimiter
private char[] multiDelimiter
-
quoteChar
private char quoteChar
-
escapeChar
private char escapeChar
-
escapeEscape
private char escapeEscape
-
quoteAllFields
private boolean quoteAllFields
-
escapeUnquoted
private boolean escapeUnquoted
-
inputNotEscaped
private boolean inputNotEscaped
-
newLine
private char newLine
-
dontProcessNormalizedNewLines
private boolean dontProcessNormalizedNewLines
-
quotationTriggers
private boolean[] quotationTriggers
-
maxTrigger
private char maxTrigger
-
quotedColumns
private java.util.Set<java.lang.Integer> quotedColumns
-
quotedFieldSelector
private FieldSelector quotedFieldSelector
-
quoteNulls
private boolean quoteNulls
-
-
Constructor Detail
-
CsvWriter
public CsvWriter(CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.Important: by not providing an instance of
Writer
to this constructor, only the operations that write to Strings are available.- Parameters:
settings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.Writer writer, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
writer
- the output resource that will receive CSV records produced by this class.settings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.File file, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
file
- the output file that will receive CSV records produced by this class.settings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.File file, java.lang.String encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
file
- the output file that will receive CSV records produced by this class.encoding
- the encoding of the filesettings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.File file, java.nio.charset.Charset encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
file
- the output file that will receive CSV records produced by this class.encoding
- the encoding of the filesettings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.OutputStream output, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
output
- the output stream that will be written with the CSV records produced by this class.settings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.OutputStream output, java.lang.String encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
output
- the output stream that will be written with the CSV records produced by this class.encoding
- the encoding of the streamsettings
- the CSV writer configuration
-
CsvWriter
public CsvWriter(java.io.OutputStream output, java.nio.charset.Charset encoding, CsvWriterSettings settings)
The CsvWriter supports all settings provided byCsvWriterSettings
, and requires this configuration to be properly initialized.- Parameters:
output
- the output stream that will be written with the CSV records produced by this class.encoding
- the encoding of the streamsettings
- the CSV writer configuration
-
-
Method Detail
-
initialize
protected final void initialize(CsvWriterSettings settings)
Initializes the CSV writer with CSV-specific configuration- Specified by:
initialize
in classAbstractWriter<CsvWriterSettings>
- Parameters:
settings
- the CSV writer configuration
-
processRow
protected void processRow(java.lang.Object[] row)
Description copied from class:AbstractWriter
Format-specific implementation for writing a single record into the output. The AbstractWriter handles the initialization and processing of the output until it is ready to be written (generally, reorganizing it and passing it on to aRowWriterProcessor
). It then delegates the record to the writer-specific implementation defined byAbstractWriter.processRow(Object[])
. In general, an implementation ofAbstractWriter.processRow(Object[])
will perform the following steps:- Iterate over each object in the given input and convert it to the expected String representation.
- The conversion must happen using the provided
AbstractWriter.appender
object. The an individual value is processed, theAbstractWriter.appendValueToRow()
method must be called. This will clear the accumulated value inAbstractWriter.appender
and add it to the output row. - Format specific separators and other characters must be introduced to the output row using
AbstractWriter.appendToRow(char)
AbstractWriter.processRow(Object[])
method returns, a row will be written to the output with the processed information, and a newline will be automatically written after the given contents, unless this is aFixedWidthWriter
whoseFixedWidthWriterSettings.getWriteLineSeparatorAfterRecord()
evaluates tofalse
. The newline character sequence will conform to what is specified inFormat.getLineSeparator()
This cycle repeats until the writing process is stopped by the user or an error happens. In case of errors, the unchecked exceptionTextWritingException
will be thrown and all resources in use will be closed automatically. The exception should contain the cause and more information about the output state when the error happened.- Specified by:
processRow
in classAbstractWriter<CsvWriterSettings>
- Parameters:
row
- the data to be written to the output in the expected format.- See Also:
CharAppender
,CommonWriterSettings
-
matchMultiDelimiter
private boolean matchMultiDelimiter(java.lang.String element, int from)
-
quoteElement
private boolean quoteElement(int start, java.lang.String element)
-
append
private boolean append(int columnIndex, boolean isElementQuoted, boolean allowTrim, java.lang.String element)
-
appendQuoted
private void appendQuoted(int start, boolean allowTrim, java.lang.String element)
-
-