Class CSVWriterBuilder
- java.lang.Object
-
- com.opencsv.CSVWriterBuilder
-
public class CSVWriterBuilder extends java.lang.Object
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
Fields Modifier and Type Field Description private java.lang.Character
escapechar
private java.lang.String
lineEnd
private ICSVParser
parser
private java.lang.Character
quotechar
private ResultSetHelper
resultSetHelper
private java.lang.Character
separator
private java.io.Writer
writer
-
Constructor Summary
Constructors Constructor Description CSVWriterBuilder(java.io.Writer writer)
Constructor taking a writer for the resulting CSV output.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ICSVWriter
build()
Creates the CSVWriter.private ICSVWriter
createCSVParserWriter()
private ICSVWriter
createCSVWriter()
CSVWriterBuilder
withEscapeChar(char escapeChar)
Sets the escape character that the ICSVWriter will be using.CSVWriterBuilder
withLineEnd(java.lang.String lineEnd)
Sets the newline character that the ICSVWriter will use.CSVWriterBuilder
withParser(ICSVParser parser)
Sets the parser that the ICSVWriter will be using.CSVWriterBuilder
withQuoteChar(char quoteChar)
Sets the quote character that the ICSVWriter will be using.CSVWriterBuilder
withResultSetHelper(ResultSetHelper helper)
Sets the ResultSetHelper that the ICSVWriter will use.CSVWriterBuilder
withSeparator(char separator)
Sets the separator that the ICSVWriter will be using.
-
-
-
Field Detail
-
writer
private final java.io.Writer writer
-
parser
private ICSVParser parser
-
separator
private java.lang.Character separator
-
quotechar
private java.lang.Character quotechar
-
escapechar
private java.lang.Character escapechar
-
resultSetHelper
private ResultSetHelper resultSetHelper
-
lineEnd
private java.lang.String lineEnd
-
-
Constructor Detail
-
CSVWriterBuilder
public CSVWriterBuilder(java.io.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
- A writer to create the resulting CSV output for the writer.
-
-
Method Detail
-
withParser
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
- Parser to inject into the ICSVWriter.- Returns:
- The CSVWriterBuilder with the parser set.
- Throws:
java.lang.IllegalArgumentException
- If a separator, quote or escape character has been set.
-
withSeparator
public CSVWriterBuilder withSeparator(char separator)
Sets the separator that the ICSVWriter will be using.- Parameters:
separator
- The separator character to use when creating the CSV content.- Returns:
- The CSVWriterBuilder with the separator set.
- Throws:
java.lang.IllegalArgumentException
- If a parser has been set.
-
withQuoteChar
public CSVWriterBuilder withQuoteChar(char quoteChar)
Sets the quote character that the ICSVWriter will be using.- Parameters:
quoteChar
- The quote character to use when creating the CSV content.- Returns:
- The CSVWriterBuilder with the quote character set.
- Throws:
java.lang.IllegalArgumentException
- If a parser has been set.
-
withEscapeChar
public CSVWriterBuilder withEscapeChar(char escapeChar)
Sets the escape character that the ICSVWriter will be using.- Parameters:
escapeChar
- The escape character to use when creating the CSV content.- Returns:
- The CSVWriterBuilder with the escape character set.
- Throws:
java.lang.IllegalArgumentException
- If a parser has been set.
-
withLineEnd
public CSVWriterBuilder withLineEnd(java.lang.String lineEnd)
Sets the newline character that the ICSVWriter will use. If none is defined then\n
will be used- Parameters:
lineEnd
- Newline string to inject into the ICSVWriter.- Returns:
- The CSVWriterBuilder with the lineEnd set.
-
build
public ICSVWriter build()
Creates the CSVWriter.- Returns:
- A CSVWriter based on the set criteria.
-
createCSVParserWriter
private ICSVWriter createCSVParserWriter()
-
createCSVWriter
private ICSVWriter createCSVWriter()
-
withResultSetHelper
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 to be injected into the ICSVWriter.- Returns:
- The CSVWriterBuiilder with the ResultSetHelper set.
-
-