Class CsvWriter

  • All Implemented Interfaces:
    RecordWriter

    public class CsvWriter
    extends java.lang.Object
    implements RecordWriter
    A CsvWriter is used to format and write records, of String arrays, to a CSV output stream. Using default settings, the CSV format supported is defined by specification RFC 4180.

    The writer also supports the following customizations:

    • The default field delimiter, ',', may be overridden.
    • The default quotation mark, '"', may be overridden.
    • The default escape character, '"', may be overridden.
    • The writer can be configured to qutoe every field. Otherwise a field is only quoted if it contains a quotation mark, delimiter, line feed or carriage return.
    Since:
    1.0
    • Constructor Summary

      Constructors 
      Constructor Description
      CsvWriter​(java.io.Writer out)
      Constructs a new CsvWriter using default settings.
      CsvWriter​(java.io.Writer out, CsvParserConfiguration config)
      Constructs a new CsvWriter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the output stream.
      void flush()
      Flushes the output stream.
      int getLineNumber()
      Returns the last line number written to the output stream.
      void write​(java.lang.Object value)
      Writes a record object to this output stream.
      void write​(java.lang.String[] record)
      Formats and writes a record to this output stream.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CsvWriter

        public CsvWriter​(java.io.Writer out)
        Constructs a new CsvWriter using default settings. according the RFC 4180 specification.
        Parameters:
        out - the output stream to write to
    • Method Detail

      • getLineNumber

        public int getLineNumber()
        Returns the last line number written to the output stream.
        Returns:
        the last line number
      • write

        public void write​(java.lang.Object value)
                   throws java.io.IOException
        Description copied from interface: RecordWriter
        Writes a record object to this output stream.
        Specified by:
        write in interface RecordWriter
        Parameters:
        value - the record object to write
        Throws:
        java.io.IOException - if an I/O error occurs writing the record to the stream
      • write

        public void write​(java.lang.String[] record)
                   throws java.io.IOException
        Formats and writes a record to this output stream.
        Parameters:
        record - the record to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • flush

        public void flush()
                   throws java.io.IOException
        Description copied from interface: RecordWriter
        Flushes the output stream.
        Specified by:
        flush in interface RecordWriter
        Throws:
        java.io.IOException - if an I/O error occurs flushing the stream
      • close

        public void close()
                   throws java.io.IOException
        Description copied from interface: RecordWriter
        Closes the output stream.
        Specified by:
        close in interface RecordWriter
        Throws:
        java.io.IOException - if an I/O error occurs closing the stream