- 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.Flushable
This is the main class for writing CSV data.Example use:
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
CsvWriter.CsvWriterBuilder
This builder is used to create configured instances ofCsvWriter
.class
CsvWriter.CsvWriterRecord
This class is used to write a record field by field.
-
Field Summary
Fields Modifier and Type Field Description private char
commentCharacter
private int
currentLineNo
private char[]
emptyFieldValue
private char
fieldSeparator
private LineDelimiter
lineDelimiter
private char[]
lineDelimiterChars
private boolean
openRecordWriter
private char
quoteCharacter
private QuoteStrategy
quoteStrategy
private Writable
writer
-
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.CsvWriterBuilder
builder()
Creates aCsvWriter.CsvWriterBuilder
instance used to configure and create instances of this class.void
close()
private boolean
containsControlCharacter(java.lang.String value, int fieldIdx, int length)
private CsvWriter
endRecord()
void
flush()
java.lang.String
toString()
private void
validateNoOpenRecord()
CsvWriter
writeComment(java.lang.String comment)
Writes a comment line and new line character(s) at the end.private void
writeCommentInternal(java.lang.String comment)
private static void
writeEscaped(Writable w, java.lang.String value, char quoteChar)
private void
writeFragment(java.lang.String comment, int i, int startPos)
private void
writeInternal(java.lang.String value, int fieldIdx)
CsvWriter.CsvWriterRecord
writeRecord()
Starts a new record.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.CsvWriter
writeRecord(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.CsvWriterBuilder
instance 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 (null
values 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 (null
values 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 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:
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:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Throws:
java.io.IOException
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-