java.lang.Object
de.siegmar.fastcsv.writer.CsvWriter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
This is the main class for writing CSV data.
Example use:
try (CsvWriter csv = CsvWriter.builder().build(file)) {
csv.writeRecord("Hello", "world");
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classThis builder is used to create configured instances ofCsvWriter.final classThis class is used to write a record field by field. -
Method Summary
Modifier and TypeMethodDescriptionstatic CsvWriter.CsvWriterBuilderbuilder()Creates aCsvWriter.CsvWriterBuilderinstance used to configure and create instances of this class.voidclose()voidflush()toString()writeComment(String comment) Writes a comment line and new line character(s) at the end.Starts a new record.writeRecord(Iterable<String> values) Writes a complete line - one or more fields and new line character(s) at the end.writeRecord(String... values) Writes a complete line - one or more fields and new line character(s) at the end.
-
Method Details
-
builder
Creates aCsvWriter.CsvWriterBuilderinstance used to configure and create instances of this class.- Returns:
- CsvWriterBuilder instance with default settings.
-
writeRecord
Writes a complete line - one or more fields and new line character(s) at the end.- Parameters:
values- the fields to write (nullvalues are handled as empty strings, if not configured otherwise (QuoteStrategies.EMPTY)).- Returns:
- This CsvWriter.
- Throws:
UncheckedIOException- if a write-error occursIllegalStateException- if a record is already started (by callingwriteRecord()) and not ended- See Also:
-
writeRecord
Writes a complete line - one or more fields and new line character(s) at the end.- Parameters:
values- the fields to write (nullvalues are handled as empty strings, if not configured otherwise (QuoteStrategies.EMPTY))- Returns:
- This CsvWriter.
- Throws:
UncheckedIOException- if a write-error occursIllegalStateException- if a record is already started (by callingwriteRecord()) and not ended- See Also:
-
writeRecord
Starts a new record.This method is used to write a record field by field. The record is ended by calling
CsvWriter.CsvWriterRecord.endRecord().- Returns:
- CsvWriterRecord instance to write fields to.
- Throws:
IllegalStateException- if a record is already started- See Also:
-
writeComment
Writes a comment line and new line character(s) at the end.Note that comments are not part of the CSV standard and may not be supported by all readers.
- Parameters:
comment- the comment to write. The comment character (configured byCsvWriter.CsvWriterBuilder.commentCharacter(char)) is automatically prepended. Empty ornullvalues results in a line only consisting of the comment character. If the argumentcommentcontains line break characters (CR, LF), multiple comment lines will be written, terminated with the line break character configured byCsvWriter.CsvWriterBuilder.lineDelimiter(LineDelimiter).- Returns:
- This CsvWriter.
- Throws:
UncheckedIOException- if a write-error occursIllegalStateException- if a record is already started (by callingwriteRecord()) and not ended
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
toString
-