- java.lang.Object
-
- de.siegmar.fastcsv.writer.CsvWriter.CsvWriterBuilder
-
- Enclosing class:
- CsvWriter
public static final class CsvWriter.CsvWriterBuilder extends java.lang.Object
This builder is used to create configured instances ofCsvWriter
. The default configuration of this class adheres with RFC 4180.- field separator:
,
(comma) - quote character:
"
(double quote) - comment character:
#
(hash/number) - quote strategy:
null
(only required quoting) - line delimiter:
LineDelimiter.CRLF
- buffer size: 8,192 bytes
- auto flush:
false
-
-
Field Summary
Fields Modifier and Type Field Description private boolean
autoFlush
private int
bufferSize
private char
commentCharacter
private static int
DEFAULT_BUFFER_SIZE
private char
fieldSeparator
private LineDelimiter
lineDelimiter
private char
quoteCharacter
private QuoteStrategy
quoteStrategy
-
Constructor Summary
Constructors Constructor Description CsvWriterBuilder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CsvWriter.CsvWriterBuilder
autoFlush(boolean autoFlush)
Configures whether data should be flushed after each record write operation.CsvWriter.CsvWriterBuilder
bufferSize(int bufferSize)
Configures the size of the internal buffer.CsvWriter
build(java.io.Writer writer)
Constructs aCsvWriter
for the specified Writer.CsvWriter
build(java.nio.file.Path file, java.nio.charset.Charset charset, java.nio.file.OpenOption... openOptions)
Constructs aCsvWriter
for the specified Path.CsvWriter
build(java.nio.file.Path file, java.nio.file.OpenOption... openOptions)
Constructs aCsvWriter
for the specified Path.CsvWriter.CsvWriterBuilder
commentCharacter(char commentCharacter)
Sets the character used to prepend commented lines – default:#
(hash/number).private CsvWriter
csvWriter(java.io.Writer writer, int bufferSize, boolean autoFlushBuffer, boolean autoFlushWriter)
CsvWriter.CsvWriterBuilder
fieldSeparator(char fieldSeparator)
Sets the character that is used to separate fields – default:,
(comma).CsvWriter.CsvWriterBuilder
lineDelimiter(LineDelimiter lineDelimiter)
Sets the delimiter used to separate lines (default:LineDelimiter.CRLF
).CsvWriter.CsvWriterBuilder
quoteCharacter(char quoteCharacter)
Sets the character used to quote values – default:"
(double quote).CsvWriter.CsvWriterBuilder
quoteStrategy(QuoteStrategy quoteStrategy)
Sets the strategy that defines when optional quoting has to be performed – default: none.CsvWriter
toConsole()
Convenience method to write to the console (standard output).java.lang.String
toString()
-
-
-
Field Detail
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
fieldSeparator
private char fieldSeparator
-
quoteCharacter
private char quoteCharacter
-
commentCharacter
private char commentCharacter
-
quoteStrategy
private QuoteStrategy quoteStrategy
-
lineDelimiter
private LineDelimiter lineDelimiter
-
bufferSize
private int bufferSize
-
autoFlush
private boolean autoFlush
-
-
Method Detail
-
fieldSeparator
public CsvWriter.CsvWriterBuilder fieldSeparator(char fieldSeparator)
Sets the character that is used to separate fields – default:,
(comma).- Parameters:
fieldSeparator
- the field separator character.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
quoteCharacter
public CsvWriter.CsvWriterBuilder quoteCharacter(char quoteCharacter)
Sets the character used to quote values – default:"
(double quote).Be aware that using characters other than the default double quote character goes against the RFC 4180 standard.
- Parameters:
quoteCharacter
- the character for enclosing fields.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
commentCharacter
public CsvWriter.CsvWriterBuilder commentCharacter(char commentCharacter)
Sets the character used to prepend commented lines – default:#
(hash/number).- Parameters:
commentCharacter
- the character for prepending commented lines.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
quoteStrategy
public CsvWriter.CsvWriterBuilder quoteStrategy(QuoteStrategy quoteStrategy)
Sets the strategy that defines when optional quoting has to be performed – default: none.- Parameters:
quoteStrategy
- the strategy when fields should be enclosed using thequoteCharacter
, even if not strictly required.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
lineDelimiter
public CsvWriter.CsvWriterBuilder lineDelimiter(LineDelimiter lineDelimiter)
Sets the delimiter used to separate lines (default:LineDelimiter.CRLF
).- Parameters:
lineDelimiter
- the line delimiter to be used.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
bufferSize
public CsvWriter.CsvWriterBuilder bufferSize(int bufferSize)
Configures the size of the internal buffer.The default buffer size of 8,192 bytes usually does not need to be altered. One use-case is if you need many instances of a CsvWriter and need to optimize for instantiation time and memory footprint.
A buffer size of 0 disables the buffer.
This setting is ignored when using
toConsole()
as console output is unbuffered.- Parameters:
bufferSize
- the buffer size to be used (must be ≥ 0).- Returns:
- This updated object, allowing additional method calls to be chained together.
-
autoFlush
public CsvWriter.CsvWriterBuilder autoFlush(boolean autoFlush)
Configures whether data should be flushed after each record write operation.Obviously this comes with drastic performance implications but can be useful for debugging purposes.
This setting is ignored when using
toConsole()
as console output is always flushed.- Parameters:
autoFlush
- whether the data should be flushed after each record write operation.- Returns:
- This updated object, allowing additional method calls to be chained together.
-
build
public CsvWriter build(java.io.Writer writer)
Constructs aCsvWriter
for the specified Writer.This library uses built-in buffering (unless
bufferSize(int)
is used to disable it) but writes its internal buffer to the givenwriter
at the end of every record write operation. Therefore, you probably want to pass in aBufferedWriter
to retain good performance. Usebuild(Path, Charset, OpenOption...)
for optimal performance when writing files!- Parameters:
writer
- the Writer to use for writing CSV data.- Returns:
- a new CsvWriter instance - never
null
. - Throws:
java.lang.NullPointerException
- if writer isnull
-
build
public CsvWriter build(java.nio.file.Path file, java.nio.file.OpenOption... openOptions) throws java.io.IOException
Constructs aCsvWriter
for the specified Path.- Parameters:
file
- the file to write data to.openOptions
- options specifying how the file is opened. SeeFiles.newOutputStream(Path, OpenOption...)
for defaults.- Returns:
- a new CsvWriter instance - never
null
. Remember to close it! - Throws:
java.io.IOException
- if a write-error occursjava.lang.NullPointerException
- if file or charset isnull
-
build
public CsvWriter build(java.nio.file.Path file, java.nio.charset.Charset charset, java.nio.file.OpenOption... openOptions) throws java.io.IOException
Constructs aCsvWriter
for the specified Path.- Parameters:
file
- the file to write data to.charset
- the character set to be used for writing data to the file.openOptions
- options specifying how the file is opened. SeeFiles.newOutputStream(Path, OpenOption...)
for defaults.- Returns:
- a new CsvWriter instance - never
null
. Remember to close it! - Throws:
java.io.IOException
- if a write-error occursjava.lang.NullPointerException
- if file or charset isnull
-
toConsole
public CsvWriter toConsole()
Convenience method to write to the console (standard output).Settings
bufferSize(int)
andautoFlush(boolean)
are ignored. Data is directly written to standard output and flushed after each record.Example use:
- Returns:
- a new CsvWriter instance - never
null
. Calls toCsvWriter.close()
are ignored, standard out remains open.
-
csvWriter
private CsvWriter csvWriter(java.io.Writer writer, int bufferSize, boolean autoFlushBuffer, boolean autoFlushWriter)
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-