Top Description Fields Constructors Methods
com.opencsv

public Class CSVWriterBuilder

extends Object
Class Inheritance
Imports
java.io.Writer

Builder for creating the CSVWriter.

Note

this should be the preferred method of creating the CSVWriter as we will no longer be creating constructors for new fields added to the writer. Plus there are now multiple flavors of CSVWriter and this will help build the correct one.


If a CSVWriterBuilder has a parser injected, it will create a CSVParserWriter, otherwise it will create a CSVWriter. If a parser is injected into a builder that already has a separator, quotechar, or escapechar then an IllegalArguementException is thrown. Likewise the opposite is true.


If nothing is defined then a CSVWriter will be produced with default settings.

Writer writer = new StringWriter(); // any Writer
CSVParser parser = new CSVParserBuilder().build();
ICSVWriter csvParserWriter = new CSVWriterBuilder(writer)
    .withParser(parser)
    .withLineEnd(ICSVWriter.RFC4180_LINE_END)
    .build(); // will produce a CSVParserWriter

ICSVWriter csvWriter = new CSVWriterBuilder(writer)
    .withSeparator(ICSVParser.DEFAULT_SEPARATOR)
    .withQuoteChar(ICSVParser.DEFAULT_QUOTE_CHARACTER)
    .withEscapeChar(ICSVParser.DEFAULT_ESCAPE_CHARACTER)
    .withLineEnd(ICSVWriter.DEFAULT_LINE_END)
    .build(); // will produce a CSVWriter

Since
4.2

Field Summary

Modifier and TypeField and Description
private Character
private String
private ICSVParser
private Character
private ResultSetHelper
private Character
private final Writer

Constructor Summary

AccessConstructor and Description
public
CSVWriterBuilder(Writer
A writer to create the resulting CSV output for the writer.
writer
)

Constructor taking a writer for the resulting CSV output.

Method Summary

Modifier and TypeMethod and Description
public ICSVWriter

Returns:

A CSVWriter based on the set criteria.
build
()

Creates the CSVWriter.

private ICSVWriter
private ICSVWriter
public CSVWriterBuilder

Returns:

The CSVWriterBuilder with the escape character set.
withEscapeChar
(char
The escape character to use when creating the CSV content.
escapeChar
)

Sets the escape character that the ICSVWriter will be using.

public CSVWriterBuilder

Returns:

The CSVWriterBuilder with the lineEnd set.
withLineEnd
(String
Newline string to inject into the ICSVWriter.
lineEnd
)

Sets the newline character that the ICSVWriter will use.

public CSVWriterBuilder

Returns:

The CSVWriterBuilder with the parser set.
withParser
(ICSVParser
Parser to inject into the ICSVWriter.
parser
)

Sets the parser that the ICSVWriter will be using.

public CSVWriterBuilder

Returns:

The CSVWriterBuilder with the quote character set.
withQuoteChar
(char
The quote character to use when creating the CSV content.
quoteChar
)

Sets the quote character that the ICSVWriter will be using.

public CSVWriterBuilder

Returns:

The CSVWriterBuiilder with the ResultSetHelper set.
withResultSetHelper
(ResultSetHelper
ResultSetHelper to be injected into the ICSVWriter.
helper
)

Sets the ResultSetHelper that the ICSVWriter will use.

public CSVWriterBuilder

Returns:

The CSVWriterBuilder with the separator set.
withSeparator
(char
The separator character to use when creating the CSV content.
separator
)

Sets the separator that the ICSVWriter will be using.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

escapecharback to summary
private Character escapechar
lineEndback to summary
private String lineEnd
parserback to summary
private ICSVParser parser
quotecharback to summary
private Character quotechar
resultSetHelperback to summary
private ResultSetHelper resultSetHelper
separatorback to summary
private Character separator
writerback to summary
private final Writer writer

Constructor Detail

CSVWriterBuilderback to summary
public CSVWriterBuilder(Writer writer)

Constructor taking a writer for the resulting CSV output. This is because the Writer is required and everything else has an optional default.

Parameters
writer:Writer

A writer to create the resulting CSV output for the writer.

Method Detail

buildback to summary
public ICSVWriter build()

Creates the CSVWriter.

Returns:ICSVWriter

A CSVWriter based on the set criteria.

createCSVParserWriterback to summary
private ICSVWriter createCSVParserWriter()
createCSVWriterback to summary
private ICSVWriter createCSVWriter()
withEscapeCharback to summary
public CSVWriterBuilder withEscapeChar(char escapeChar)

Sets the escape character that the ICSVWriter will be using.

Parameters
escapeChar:char

The escape character to use when creating the CSV content.

Returns:CSVWriterBuilder

The CSVWriterBuilder with the escape character set.

Exceptions
IllegalArgumentException:
If a parser has been set.
withLineEndback to summary
public CSVWriterBuilder withLineEnd(String lineEnd)

Sets the newline character that the ICSVWriter will use. If none is defined then \n will be used

Parameters
lineEnd:String

Newline string to inject into the ICSVWriter.

Returns:CSVWriterBuilder

The CSVWriterBuilder with the lineEnd set.

withParserback to summary
public CSVWriterBuilder withParser(ICSVParser parser)

Sets the parser that the ICSVWriter will be using. If none is defined then a CSVWriter will be returned when the build command is executed.

Parameters
parser:ICSVParser

Parser to inject into the ICSVWriter.

Returns:CSVWriterBuilder

The CSVWriterBuilder with the parser set.

Exceptions
IllegalArgumentException:
If a separator, quote or escape character has been set.
withQuoteCharback to summary
public CSVWriterBuilder withQuoteChar(char quoteChar)

Sets the quote character that the ICSVWriter will be using.

Parameters
quoteChar:char

The quote character to use when creating the CSV content.

Returns:CSVWriterBuilder

The CSVWriterBuilder with the quote character set.

Exceptions
IllegalArgumentException:
If a parser has been set.
withResultSetHelperback to summary
public CSVWriterBuilder withResultSetHelper(ResultSetHelper helper)

Sets the ResultSetHelper that the ICSVWriter will use. If it is not defined then it will not be set and will be up to the ICSVWriter to handle - CSVWriter will create one by default.

Parameters
helper:ResultSetHelper

ResultSetHelper to be injected into the ICSVWriter.

Returns:CSVWriterBuilder

The CSVWriterBuiilder with the ResultSetHelper set.

withSeparatorback to summary
public CSVWriterBuilder withSeparator(char separator)

Sets the separator that the ICSVWriter will be using.

Parameters
separator:char

The separator character to use when creating the CSV content.

Returns:CSVWriterBuilder

The CSVWriterBuilder with the separator set.

Exceptions
IllegalArgumentException:
If a parser has been set.