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: null (only required quoting)
  • line delimiter: LineDelimiter.CRLF
  • buffer size: 8,192 bytes
  • auto flush: false
  • Field Details

    • DEFAULT_BUFFER_SIZE

      private static final int DEFAULT_BUFFER_SIZE
      See Also:
    • fieldSeparator

      private char fieldSeparator
    • quoteCharacter

      private char quoteCharacter
    • commentCharacter

      private char commentCharacter
    • quoteStrategy

      private QuoteStrategy quoteStrategy
    • lineDelimiter

      private LineDelimiter lineDelimiter
    • bufferSize

      private int bufferSize
    • autoFlush

      private boolean autoFlush
  • Constructor Details

    • CsvWriterBuilder

      CsvWriterBuilder()
  • 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: none.
      Parameters:
      quoteStrategy - the strategy when fields should be enclosed using the quoteCharacter, even if not strictly required.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • 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.
      Returns:
      This updated object, allowing additional method calls to be chained together.
    • 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(Writer writer)
      Constructs a CsvWriter for the specified Writer.

      This library uses built-in buffering (unless bufferSize(int) is used to disable it) but writes its internal buffer to the given writer at the end of every record write operation. Therefore, you probably want to pass in a BufferedWriter to retain good performance. 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.
      Throws:
      NullPointerException - if writer is null
    • build

      public CsvWriter build(Path file, OpenOption... openOptions) throws IOException
      Constructs a CsvWriter for the specified Path.
      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.
    • csvWriter

      private CsvWriter csvWriter(Writer writer, int bufferSize, boolean autoFlushBuffer, boolean autoFlushWriter)
    • toString

      public String toString()
      Overrides:
      toString in class Object