Class 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:

    • Field Detail

      • fieldSeparator

        private final char fieldSeparator
      • quoteCharacter

        private final char quoteCharacter
      • commentCharacter

        private final char commentCharacter
      • 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

      • 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 occurs
        java.lang.IllegalStateException - if a record is already started (by calling writeRecord()) 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 occurs
        java.lang.IllegalStateException - if a record is already started (by calling writeRecord()) and not ended
        See Also:
        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 by CsvWriter.CsvWriterBuilder.commentCharacter(char)) is automatically prepended. Empty or null values results in a line only consisting of the comment character. If the argument comment contains line break characters (CR, LF), multiple comment lines will be written, terminated with the line break character configured by CsvWriter.CsvWriterBuilder.lineDelimiter(LineDelimiter).
        Returns:
        This CsvWriter.
        Throws:
        java.io.UncheckedIOException - if a write-error occurs
        java.lang.IllegalStateException - if a record is already started (by calling writeRecord()) 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 interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • flush

        public void flush()
                   throws java.io.IOException
        Specified by:
        flush in interface java.io.Flushable
        Throws:
        java.io.IOException
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object