- Enclosing class:
CsvWriter
CsvWriter
. 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
FieldsModifier and TypeFieldDescriptionprivate boolean
private int
private char
private static final int
private char
private LineDelimiter
private char
private QuoteStrategy
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionautoFlush
(boolean autoFlush) Configures whether data should be flushed after each record write operation.bufferSize
(int bufferSize) Configures the size of the internal buffer.Constructs aCsvWriter
for the specified Writer.build
(Path file, Charset charset, OpenOption... openOptions) Constructs aCsvWriter
for the specified Path.build
(Path file, OpenOption... openOptions) Constructs aCsvWriter
for the specified Path.commentCharacter
(char commentCharacter) Sets the character used to prepend commented lines – default:#
(hash/number).private CsvWriter
fieldSeparator
(char fieldSeparator) Sets the character that is used to separate fields – default:,
(comma).lineDelimiter
(LineDelimiter lineDelimiter) Sets the delimiter used to separate lines (default:LineDelimiter.CRLF
).quoteCharacter
(char quoteCharacter) Sets the character used to quote values – default:"
(double quote).quoteStrategy
(QuoteStrategy quoteStrategy) Sets the strategy that defines when optional quoting has to be performed – default: none.Convenience method to write to the console (standard output).toString()
-
Field Details
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE- See Also:
-
fieldSeparator
private char fieldSeparator -
quoteCharacter
private char quoteCharacter -
commentCharacter
private char commentCharacter -
quoteStrategy
-
lineDelimiter
-
bufferSize
private int bufferSize -
autoFlush
private boolean autoFlush
-
-
Constructor Details
-
CsvWriterBuilder
CsvWriterBuilder()
-
-
Method Details
-
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
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
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
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
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
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
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
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:
NullPointerException
- if writer isnull
-
build
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:
IOException
- if a write-error occursNullPointerException
- if file or charset isnull
-
build
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:
IOException
- if a write-error occursNullPointerException
- if file or charset isnull
-
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:
CsvWriter.builder().toConsole() .writeRecord("Hello", "world");
- Returns:
- a new CsvWriter instance - never
null
. Calls toCsvWriter.close()
are ignored, standard out remains open.
-
csvWriter
-
toString
-