- java.lang.Object
-
- de.siegmar.fastcsv.writer.CsvWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public final class CsvWriter extends java.lang.Object implements java.io.Closeable, java.io.FlushableThis is the main class for writing CSV data.Example use:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCsvWriter.CsvWriterBuilderThis builder is used to create configured instances ofCsvWriter.classCsvWriter.CsvWriterRecordThis class is used to write a record field by field.
-
Field Summary
Fields Modifier and Type Field Description private charcommentCharacterprivate intcurrentLineNoprivate char[]emptyFieldValueprivate charfieldSeparatorprivate LineDelimiterlineDelimiterprivate char[]lineDelimiterCharsprivate booleanopenRecordWriterprivate charquoteCharacterprivate QuoteStrategyquoteStrategyprivate Writablewriter
-
Constructor Summary
Constructors Constructor Description CsvWriter(Writable writer, char fieldSeparator, char quoteCharacter, char commentCharacter, QuoteStrategy quoteStrategy, LineDelimiter lineDelimiter)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static CsvWriter.CsvWriterBuilderbuilder()Creates aCsvWriter.CsvWriterBuilderinstance used to configure and create instances of this class.voidclose()private booleancontainsControlCharacter(java.lang.String value, int fieldIdx, int length)private CsvWriterendRecord()voidflush()java.lang.StringtoString()private voidvalidateNoOpenRecord()CsvWriterwriteComment(java.lang.String comment)Writes a comment line and new line character(s) at the end.private voidwriteCommentInternal(java.lang.String comment)private static voidwriteEscaped(Writable w, java.lang.String value, char quoteChar)private voidwriteFragment(java.lang.String comment, int i, int startPos)private voidwriteInternal(java.lang.String value, int fieldIdx)CsvWriter.CsvWriterRecordwriteRecord()Starts a new record.CsvWriterwriteRecord(java.lang.Iterable<java.lang.String> values)Writes a complete line - one or more fields and new line character(s) at the end.CsvWriterwriteRecord(java.lang.String... values)Writes a complete line - one or more fields and new line character(s) at the end.
-
-
-
Field Detail
-
writer
private final Writable writer
-
fieldSeparator
private final char fieldSeparator
-
quoteCharacter
private final char quoteCharacter
-
commentCharacter
private final char commentCharacter
-
quoteStrategy
private final QuoteStrategy quoteStrategy
-
lineDelimiter
private final LineDelimiter lineDelimiter
-
currentLineNo
private int currentLineNo
-
lineDelimiterChars
private final char[] lineDelimiterChars
-
emptyFieldValue
private final char[] emptyFieldValue
-
openRecordWriter
private boolean openRecordWriter
-
-
Constructor Detail
-
CsvWriter
CsvWriter(Writable writer, char fieldSeparator, char quoteCharacter, char commentCharacter, QuoteStrategy quoteStrategy, LineDelimiter lineDelimiter)
-
-
Method Detail
-
builder
public static CsvWriter.CsvWriterBuilder builder()
Creates aCsvWriter.CsvWriterBuilderinstance used to configure and create instances of this class.- Returns:
- CsvWriterBuilder instance with default settings.
-
writeRecord
public CsvWriter writeRecord(java.lang.Iterable<java.lang.String> values)
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:
java.io.UncheckedIOException- if a write-error occursjava.lang.IllegalStateException- if a record is already started (by callingwriteRecord()) and not ended- See Also:
writeRecord(String...)
-
writeRecord
public CsvWriter writeRecord(java.lang.String... values)
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:
java.io.UncheckedIOException- if a write-error occursjava.lang.IllegalStateException- if a record is already started (by callingwriteRecord()) and not ended- See Also:
writeRecord(Iterable)
-
writeRecord
public CsvWriter.CsvWriterRecord 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:
java.lang.IllegalStateException- if a record is already started- See Also:
writeRecord(String...),writeRecord(Iterable)
-
validateNoOpenRecord
private void validateNoOpenRecord()
-
writeInternal
private void writeInternal(java.lang.String value, int fieldIdx) throws java.io.IOException- Throws:
java.io.IOException
-
containsControlCharacter
private boolean containsControlCharacter(java.lang.String value, int fieldIdx, int length)
-
writeEscaped
private static void writeEscaped(Writable w, java.lang.String value, char quoteChar) throws java.io.IOException
- Throws:
java.io.IOException
-
writeComment
public CsvWriter writeComment(java.lang.String comment)
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:
java.io.UncheckedIOException- if a write-error occursjava.lang.IllegalStateException- if a record is already started (by callingwriteRecord()) and not ended
-
writeCommentInternal
private void writeCommentInternal(java.lang.String comment) throws java.io.IOException- Throws:
java.io.IOException
-
writeFragment
private void writeFragment(java.lang.String comment, int i, int startPos) throws java.io.IOException- Throws:
java.io.IOException
-
endRecord
private CsvWriter endRecord() throws java.io.IOException
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException- Specified by:
flushin interfacejava.io.Flushable- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-