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 class
This builder is used to create configured instances ofCsvWriter
.final class
This class is used to write a record field by field. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final char
private int
private final char[]
private final char
private final LineDelimiter
private final char[]
private boolean
private final char
private final QuoteStrategy
private final Writable
-
Constructor Summary
ConstructorsConstructorDescriptionCsvWriter
(Writable writer, char fieldSeparator, char quoteCharacter, char commentCharacter, QuoteStrategy quoteStrategy, LineDelimiter lineDelimiter) -
Method Summary
Modifier and TypeMethodDescriptionstatic CsvWriter.CsvWriterBuilder
builder()
Creates aCsvWriter.CsvWriterBuilder
instance used to configure and create instances of this class.void
close()
private boolean
containsControlCharacter
(String value, int fieldIdx, int length) private CsvWriter
void
flush()
toString()
private void
writeComment
(String comment) Writes a comment line and new line character(s) at the end.private void
writeCommentInternal
(String comment) private static void
writeEscaped
(Writable w, String value, char quoteChar) private void
writeFragment
(String comment, int i, int startPos) private void
writeInternal
(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.CsvWriterBuilder
instance 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 (null
values 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 (null
values 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 ornull
values results in a line only consisting of the comment character. If the argumentcomment
contains 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
flush
- Specified by:
flush
in interfaceFlushable
- Throws:
IOException
-
toString
-