Class CsvWriter.CsvWriterBuilder

java.lang.Object
de.siegmar.fastcsv.writer.CsvWriter.CsvWriterBuilder
Enclosing class:
CsvWriter

public static final class CsvWriter.CsvWriterBuilder extends Object

This builder is used to create configured instances of CsvWriter. The default configuration of this class adheres with RFC 4180.

  • field separator: , (comma)
  • quote character: " (double quote)
  • comment character: # (hash/number)
  • quote strategy: QuoteStrategies.REQUIRED
  • line delimiter: LineDelimiter.CRLF
  • buffer size: 8,192 bytes
  • auto flush: false
  • Method Details

    • fieldSeparator

      public CsvWriter.CsvWriterBuilder fieldSeparator(char fieldSeparator)
      Sets the character that is used to separate fields – default: , (comma).
      Parameters:
      fieldSeparator - the field separator character.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • quoteCharacter

      public CsvWriter.CsvWriterBuilder quoteCharacter(char quoteCharacter)

      Sets the character used to quote values – default: " (double quote).

      Be aware that using characters other than the default double quote character goes against the RFC 4180 standard.

      Parameters:
      quoteCharacter - the character for enclosing fields.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • commentCharacter

      public CsvWriter.CsvWriterBuilder commentCharacter(char commentCharacter)
      Sets the character used to prepend commented lines – default: # (hash/number).
      Parameters:
      commentCharacter - the character for prepending commented lines.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • quoteStrategy

      public CsvWriter.CsvWriterBuilder quoteStrategy(QuoteStrategy quoteStrategy)
      Sets the strategy that defines when optional quoting has to be performed (default: QuoteStrategies.REQUIRED).
      Parameters:
      quoteStrategy - the strategy when fields should be enclosed using the quoteCharacter, even if not strictly required; must not be null.
      Returns:
      This updated object, allowing additional method calls to be chained together.
      Throws:
      NullPointerException - if quoteStrategy is null
      See Also:
    • lineDelimiter

      public CsvWriter.CsvWriterBuilder lineDelimiter(LineDelimiter lineDelimiter)
      Sets the delimiter used to separate lines (default: LineDelimiter.CRLF).
      Parameters:
      lineDelimiter - the line delimiter to be used; must not be null.
      Returns:
      This updated object, allowing additional method calls to be chained together.
      Throws:
      NullPointerException - if lineDelimiter is null
    • bufferSize

      public CsvWriter.CsvWriterBuilder bufferSize(int bufferSize)

      Configures the size of the internal buffer.

      The default buffer size of 8,192 bytes usually does not need to be altered. One use-case is if you need many instances of a CsvWriter and need to optimize for instantiation time and memory footprint.

      A buffer size of 0 disables the buffer.

      This setting is ignored when using toConsole() as console output is unbuffered.

      Parameters:
      bufferSize - the buffer size to be used (must be ≥ 0).
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • autoFlush

      public CsvWriter.CsvWriterBuilder autoFlush(boolean autoFlush)

      Configures whether data should be flushed after each record write operation.

      Obviously this comes with drastic performance implications but can be useful for debugging purposes.

      This setting is ignored when using toConsole() as console output is always flushed.

      Parameters:
      autoFlush - whether the data should be flushed after each record write operation.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • build

      public CsvWriter build(OutputStream outputStream)

      Constructs a CsvWriter for the specified OutputStream.

      See build(OutputStream, Charset) for details. This is just a convenience method for calling it with charset set to StandardCharsets.UTF_8.

      Parameters:
      outputStream - the OutputStream to write CSV data to.
      Returns:
      a new CsvWriter instance - never null. Remember to close it!
      Throws:
      NullPointerException - if outputStream is null
      See Also:
    • build

      public CsvWriter build(OutputStream outputStream, Charset charset)

      Constructs a CsvWriter for the specified OutputStream and character set.

      This build method wraps the given outputStream with an OutputStreamWriter. Both this library's internal buffer and the used OutputStreamWriter cause deferred writes to the underlying stream. You typically do not need to wrap the given outputStream in a BufferedOutputStream. This ensures good performance but also means that you must call CsvWriter.flush() or CsvWriter.close() to ensure that all data is written to the underlying outputStream!

      Use build(Path,Charset,OpenOption...) for optimal performance when writing files!

      Parameters:
      outputStream - the OutputStream to write CSV data to.
      charset - the character set to be used for writing data to the output stream.
      Returns:
      a new CsvWriter instance - never null. Remember to close it!
      Throws:
      NullPointerException - if outputStream or charset is null
      See Also:
    • build

      public CsvWriter build(Writer writer)

      Constructs a CsvWriter for the specified Writer.

      This library uses built-in buffering, unless bufferSize(int) is used to disable it. You typically do not need to wrap the given writer in an BufferedWriter. This ensures good performance but also means that you must call CsvWriter.flush() or CsvWriter.close() to ensure that all data is written to the underlying writer!

      Use build(Path,Charset,OpenOption...) for optimal performance when writing files!

      Parameters:
      writer - the Writer to use for writing CSV data.
      Returns:
      a new CsvWriter instance - never null. Remember to close it!
      Throws:
      NullPointerException - if writer is null
    • build

      public CsvWriter build(Path file, OpenOption... openOptions) throws IOException

      Constructs a CsvWriter for the specified Path.

      See build(Path,Charset,OpenOption...) for details. This is just a convenience method for calling it with charset set to StandardCharsets.UTF_8.

      Parameters:
      file - the file to write data to.
      openOptions - options specifying how the file is opened. See Files.newOutputStream(Path,OpenOption...) for defaults.
      Returns:
      a new CsvWriter instance - never null. Remember to close it!
      Throws:
      IOException - if a write-error occurs
      NullPointerException - if file or charset is null
    • build

      public CsvWriter build(Path file, Charset charset, OpenOption... openOptions) throws IOException
      Constructs a CsvWriter for the specified Path.
      Parameters:
      file - the file to write data to.
      charset - the character set to be used for writing data to the file.
      openOptions - options specifying how the file is opened. See Files.newOutputStream(Path,OpenOption...) for defaults.
      Returns:
      a new CsvWriter instance - never null. Remember to close it!
      Throws:
      IOException - if a write-error occurs
      NullPointerException - if file or charset is null
    • toConsole

      public CsvWriter toConsole()

      Convenience method to write to the console (standard output).

      Settings bufferSize(int) and autoFlush(boolean) are ignored. Data is directly written to standard output and flushed after each record.

      Example use:

      CsvWriter.builder().toConsole()
          .writeRecord("Hello", "world");
      
      Returns:
      a new CsvWriter instance - never null. Calls to CsvWriter.close() are ignored, standard out remains open.
    • toString

      public String toString()
      Overrides:
      toString in class Object