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. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final charprivate intprivate final char[]private final charprivate final LineDelimiterprivate final char[]private booleanprivate final charprivate final QuoteStrategyprivate final Writable -
Constructor Summary
ConstructorsConstructorDescriptionCsvWriter(Writable writer, char fieldSeparator, char quoteCharacter, char commentCharacter, QuoteStrategy quoteStrategy, LineDelimiter lineDelimiter) -
Method Summary
Modifier and TypeMethodDescriptionstatic CsvWriter.CsvWriterBuilderbuilder()Creates aCsvWriter.CsvWriterBuilderinstance used to configure and create instances of this class.voidclose()private booleancontainsControlCharacter(String value, int fieldIdx, int length) private CsvWritervoidflush()toString()private voidwriteComment(String comment) Writes a comment line and new line character(s) at the end.private voidwriteCommentInternal(String comment) private static voidwriteEscaped(Writable w, String value, char quoteChar) private voidwriteFragment(String comment, int i, int startPos) private voidwriteInternal(String value, int fieldIdx) 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.
-
Field Details
-
writer
-
fieldSeparator
private final char fieldSeparator -
quoteCharacter
private final char quoteCharacter -
commentCharacter
private final char commentCharacter -
quoteStrategy
-
lineDelimiter
-
currentLineNo
private int currentLineNo -
lineDelimiterChars
private final char[] lineDelimiterChars -
emptyFieldValue
private final char[] emptyFieldValue -
openRecordWriter
private boolean openRecordWriter
-
-
Constructor Details
-
CsvWriter
CsvWriter(Writable writer, char fieldSeparator, char quoteCharacter, char commentCharacter, QuoteStrategy quoteStrategy, LineDelimiter lineDelimiter)
-
-
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:
-
validateNoOpenRecord
private void validateNoOpenRecord() -
writeInternal
- Throws:
IOException
-
containsControlCharacter
-
writeEscaped
- Throws:
IOException
-
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
-
writeCommentInternal
- Throws:
IOException
-
writeFragment
- Throws:
IOException
-
endRecord
- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
toString
-