Top Description Fields Constructors Methods
com.univocity.parsers.csv

public Class CsvWriter

extends AbstractWriter<CsvWriterSettings>
Class Inheritance
Imports
com.univocity.parsers.common.*, com.univocity.parsers.common.fields.*, java.io.*, java.nio.charset.*, java.util.*

A powerful and flexible CSV writer implementation.
Author
Univocity Software Pty Ltd - [email protected]
See Also
CsvFormat, CsvWriterSettings, CsvParser, AbstractWriter

Field Summary

Modifier and TypeField and Description
private char
private boolean
private char
private char
private boolean
private boolean
private char
private char[]
private char
private boolean[]
private boolean
private char
private Set<Integer>
private FieldSelector
private boolean
Inherited from com.univocity.parsers.common.AbstractWriter:
appendercommentemptyValueexpandRowsheadersheaderTrimFlagsignoreLeadingignoreTrailingnullValuerecordCountusingNullOrEmptyValuewhitespaceRangeStartwritingHeaders

Constructor Summary

AccessConstructor and Description
public
CsvWriter(CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(Writer
the output resource that will receive CSV records produced by this class.
writer
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(File
the output file that will receive CSV records produced by this class.
file
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(File
the output file that will receive CSV records produced by this class.
file
,
String
the encoding of the file
encoding
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(File
the output file that will receive CSV records produced by this class.
file
,
Charset
the encoding of the file
encoding
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(OutputStream
the output stream that will be written with the CSV records produced by this class.
output
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(OutputStream
the output stream that will be written with the CSV records produced by this class.
output
,
String
the encoding of the stream
encoding
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

public
CsvWriter(OutputStream
the output stream that will be written with the CSV records produced by this class.
output
,
Charset
the encoding of the stream
encoding
,
CsvWriterSettings
the CSV writer configuration
settings
)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Method Summary

Modifier and TypeMethod and Description
private boolean
append(int columnIndex, boolean isElementQuoted, boolean allowTrim, String element)

private void
appendQuoted(int start, boolean allowTrim, String element)

protected final void
initialize(CsvWriterSettings
the CSV writer configuration
settings
)

Implements abstract com.univocity.parsers.common.AbstractWriter.initialize.

Initializes the CSV writer with CSV-specific configuration
private boolean
matchMultiDelimiter(String element, int from)

protected void
processRow(Object[]
the data to be written to the output in the expected format.
row
)

Implements abstract com.univocity.parsers.common.AbstractWriter.processRow.

Format-specific implementation for writing a single record into the output.
private boolean
quoteElement(int start, String element)

Inherited from com.univocity.parsers.common.AbstractWriter:
addStringValuesaddValueaddValueaddValueaddValuesaddValuesallowTrimappendToRowappendToRowappendValueToRowclosecommentRowcommentRowToStringdiscardValuesenableNewlineAfterRecordexpandflushgetRecordCountgetStringValueprocessObjectRecordsprocessObjectRecordsAndCloseprocessObjectRecordsAndCloseprocessObjectRecordsToStringprocessObjectRecordsToStringprocessRecordprocessRecordprocessRecordprocessRecordprocessRecordprocessRecordsprocessRecordsprocessRecordsprocessRecordsprocessRecordsprocessRecordsAndCloseprocessRecordsAndCloseprocessRecordsAndCloseprocessRecordsAndCloseprocessRecordsToStringprocessRecordsToStringprocessRecordsToStringprocessRecordsToStringprocessRecordToStringprocessRecordToStringprocessRecordToStringprocessRecordToStringprocessRecordToStringprocessValuesToRowprocessValuesToStringskipLeadingWhitespaceupdateFieldExclusionupdateFieldExclusionupdateFieldSelectionupdateFieldSelectionwriteEmptyRowwriteHeaderswriteHeaderswriteHeaderswriteHeadersToStringwriteHeadersToStringwriteHeadersToStringwriteObjectRowswriteObjectRowsAndClosewriteObjectRowsAndClosewriteObjectRowsToStringwriteRecordwriteRecordswriteRecordswriteRecordsAndClosewriteRecordsAndClosewriteRecordsAndClosewriteRecordsToStringwriteRecordsToStringwriteRecordToStringwriteRowwriteRowwriteRowwriteRowwriteRowwriteRowwriteRowswriteRowswriteRowswriteRowswriteRowswriteRowsAndClosewriteRowsAndClosewriteRowsAndClosewriteRowsAndClosewriteRowsAndClosewriteRowsToStringwriteRowsToStringwriteRowsToStringwriteRowsToStringwriteRowsToStringwriteRowToStringwriteRowToStringwriteRowToStringwriteRowToStringwriteRowToStringwriteStringRowswriteStringRowswriteStringRowswriteStringRowsAndClosewriteStringRowsAndClosewriteStringRowsAndClosewriteStringRowsToStringwriteStringRowsToStringwriteStringRowsToStringwriteValuesToRowwriteValuesToString

Field Detail

delimiterback to summary
private char delimiter
dontProcessNormalizedNewLinesback to summary
private boolean dontProcessNormalizedNewLines
escapeCharback to summary
private char escapeChar
escapeEscapeback to summary
private char escapeEscape
escapeUnquotedback to summary
private boolean escapeUnquoted
inputNotEscapedback to summary
private boolean inputNotEscaped
maxTriggerback to summary
private char maxTrigger
multiDelimiterback to summary
private char[] multiDelimiter
newLineback to summary
private char newLine
quotationTriggersback to summary
private boolean[] quotationTriggers
quoteAllFieldsback to summary
private boolean quoteAllFields
quoteCharback to summary
private char quoteChar
quotedColumnsback to summary
private Set<Integer> quotedColumns
quotedFieldSelectorback to summary
private FieldSelector quotedFieldSelector
quoteNullsback to summary
private boolean quoteNulls

Constructor Detail

CsvWriterback to summary
public CsvWriter(CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Important Note

by not providing an instance of java.io.Writer to this constructor, only the operations that write to Strings are available.

Parameters
settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(Writer writer, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
writer:Writer

the output resource that will receive CSV records produced by this class.

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(File file, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
file:File

the output file that will receive CSV records produced by this class.

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(File file, String encoding, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
file:File

the output file that will receive CSV records produced by this class.

encoding:String

the encoding of the file

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(File file, Charset encoding, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
file:File

the output file that will receive CSV records produced by this class.

encoding:Charset

the encoding of the file

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(OutputStream output, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
output:OutputStream

the output stream that will be written with the CSV records produced by this class.

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(OutputStream output, String encoding, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
output:OutputStream

the output stream that will be written with the CSV records produced by this class.

encoding:String

the encoding of the stream

settings:CsvWriterSettings

the CSV writer configuration

CsvWriterback to summary
public CsvWriter(OutputStream output, Charset encoding, CsvWriterSettings settings)

The CsvWriter supports all settings provided by CsvWriterSettings, and requires this configuration to be properly initialized.

Parameters
output:OutputStream

the output stream that will be written with the CSV records produced by this class.

encoding:Charset

the encoding of the stream

settings:CsvWriterSettings

the CSV writer configuration

Method Detail

appendback to summary
private boolean append(int columnIndex, boolean isElementQuoted, boolean allowTrim, String element)
appendQuotedback to summary
private void appendQuoted(int start, boolean allowTrim, String element)
initializeback to summary
protected final void initialize(CsvWriterSettings settings)

Implements abstract com.univocity.parsers.common.AbstractWriter.initialize.

Initializes the CSV writer with CSV-specific configuration

Parameters
settings:CsvWriterSettings

the CSV writer configuration

matchMultiDelimiterback to summary
private boolean matchMultiDelimiter(String element, int from)
processRowback to summary
protected void processRow(Object[] row)

Implements abstract com.univocity.parsers.common.AbstractWriter.processRow.

Doc from com.univocity.parsers.common.AbstractWriter.processRow.

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 a RowWriterProcessor). It then delegates the record to the writer-specific implementation defined by processRow(Object[]). In general, an implementation of AbstractWriter#processRow(Object[]) will perform the following steps:

Once the 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 a com.univocity.parsers.fixed.FixedWidthWriter whose FixedWidthWriterSettings#getWriteLineSeparatorAfterRecord() evaluates to false. The newline character sequence will conform to what is specified in Format#getLineSeparator() This cycle repeats until the writing process is stopped by the user or an error happens. In case of errors, the unchecked exception TextWritingException 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.
Parameters
row:Object[]

the data to be written to the output in the expected format.

Annotations
@Override
quoteElementback to summary
private boolean quoteElement(int start, String element)