Class CsvWriter.CsvWriterBuilder

  • Enclosing class:
    CsvWriter

    public static final class CsvWriter.CsvWriterBuilder
    extends java.lang.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 Detail

      • fieldSeparator

        private char fieldSeparator
      • quoteCharacter

        private char quoteCharacter
      • commentCharacter

        private char commentCharacter
      • bufferSize

        private int bufferSize
      • autoFlush

        private boolean autoFlush
    • Constructor Detail

      • CsvWriterBuilder

        CsvWriterBuilder()
    • Method Detail

      • 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​(java.io.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:
        java.lang.NullPointerException - if writer is null
      • build

        public CsvWriter build​(java.nio.file.Path file,
                               java.nio.file.OpenOption... openOptions)
                        throws java.io.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:
        java.io.IOException - if a write-error occurs
        java.lang.NullPointerException - if file or charset is null
      • build

        public CsvWriter build​(java.nio.file.Path file,
                               java.nio.charset.Charset charset,
                               java.nio.file.OpenOption... openOptions)
                        throws java.io.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:
        java.io.IOException - if a write-error occurs
        java.lang.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:

        Returns:
        a new CsvWriter instance - never null. Calls to CsvWriter.close() are ignored, standard out remains open.
      • csvWriter

        private CsvWriter csvWriter​(java.io.Writer writer,
                                    int bufferSize,
                                    boolean autoFlushBuffer,
                                    boolean autoFlushWriter)
      • toString

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