Class CSVWriter


public class CSVWriter extends LinedTextWriter
Writes records to a Comma Separated Value (CSV) stream.
  • Constructor Details

    • CSVWriter

      public CSVWriter(File file, boolean append)
      Parameters:
      file -
      append - Indicates if writing should start at the end of the file (if it exists). If the specified file exists and has contents (more than 0 bytes), then the fieldNamesInFirstRow property will be set to false.
    • CSVWriter

      public CSVWriter(File file)
    • CSVWriter

      public CSVWriter(Writer writer)
  • Method Details

    • addExceptionProperties

      public DataException addExceptionProperties(DataException exception)
      Description copied from class: Endpoint
      Adds this endpoint's current state to a DataException. Since this method is called whenever an exception is thrown, subclasses should override it to add their specific information.
      Overrides:
      addExceptionProperties in class LinedTextWriter
    • setNewLine

      public CSVWriter setNewLine(String newLine)
      Assigns the string used between each line (defaults to "\n" on Linux and "\r\n" on Windows).
      Overrides:
      setNewLine in class LinedTextWriter
    • setForceQuote

      public CSVWriter setForceQuote(boolean force)
      Indicates if all values should be quoted, even if they are not null or empty strings.
    • isForceQuote

      public boolean isForceQuote()
      Returns true if all values will be quoted, even if they are not null or empty strings.
      Returns:
      True if enabled, false otherwise
    • getFieldSeparator

      public String getFieldSeparator()
      Returns the string used between each field value (defaults to comma).
    • setFieldSeparator

      public CSVWriter setFieldSeparator(char fieldSeparator)
      Assigns the string used between each field value (defaults to comma).
    • setFieldSeparator

      public CSVWriter setFieldSeparator(String fieldSeparator)
      Assigns the string used between each field value (defaults to comma).
    • getQuoteChar

      public String getQuoteChar()
      Returns the starting quote - same as calling getStartingQuote() (defaults to double quote - "). getStartingQuote()
    • setQuoteChar

      public CSVWriter setQuoteChar(String quoteChar)
      Assigns startingQuote and endingQuote to the same value (defaults to double quote - "). setStartingQuote(String) setEndingQuote(String)
    • getStartingQuote

      public String getStartingQuote()
      Returns the string used to mark the beginning of text values that may contain commas and other special characters (defaults to double quote - ").
    • setStartingQuote

      public CSVWriter setStartingQuote(String startingQuote)
      Assigns the string used to mark the beginning of text values that may contain commas and other special characters (defaults to double quote - ").
    • getEndingQuote

      public String getEndingQuote()
      Returns the string used to mark the ending of text values that may contain commas and other special characters (defaults to double quote - ").
    • setEndingQuote

      public CSVWriter setEndingQuote(String endingQuote)
      Assigns the string used to mark the ending of text values that may contain commas and other special characters (defaults to double quote - ").
    • getNullValuePolicy

      public CSVWriter.IValuePolicy getNullValuePolicy()
      Returns the object responsible for writing null values (defaults to ValuePolicy.EMPTY_STRING).
      See Also:
    • setNullValuePolicy

      public CSVWriter setNullValuePolicy(CSVWriter.IValuePolicy nullValuePolicy)
      Assigns the object responsible for writing null values (defaults to ValuePolicy.EMPTY_STRING).
      See Also:
    • getEmptyStringValuePolicy

      public CSVWriter.IValuePolicy getEmptyStringValuePolicy()
      Returns the object responsible for writing empty string values (defaults to ValuePolicy.QUOTED_EMPTY_STRING).
      See Also:
    • setEmptyStringValuePolicy

      public CSVWriter setEmptyStringValuePolicy(CSVWriter.IValuePolicy emptyStringValuePolicy)
      Assigns the object responsible for writing empty string values (defaults to ValuePolicy.QUOTED_EMPTY_STRING).
      See Also:
    • setAutoCloseWriter

      public CSVWriter setAutoCloseWriter(boolean autoCloseWriter)
      Description copied from class: TextWriter
      Indicates if the underlying java.io.BufferedWriter should be closed when this stream closes (defaults to true).
      Overrides:
      setAutoCloseWriter in class TextWriter
    • setFlushOnWrite

      public CSVWriter setFlushOnWrite(boolean flushOnWrite)
      Description copied from class: TextWriter
      Indicates if the underlying java.io.BufferedWriter should be flushed after each record is written (defaults to false).
      Overrides:
      setFlushOnWrite in class TextWriter
    • setFieldNamesInFirstRow

      public CSVWriter setFieldNamesInFirstRow(boolean fieldNamesInFirstRow)
      Description copied from class: AbstractWriter
      Indicates if a header row of field names is written before the first record (default is true); must be called before AbstractWriter.open().
      Overrides:
      setFieldNamesInFirstRow in class AbstractWriter
    • setDescription

      public CSVWriter setDescription(String description)
      Description copied from class: Endpoint
      Sets the optional text shown for this endpoint in Endpoint.toString() and exception properties.
      Overrides:
      setDescription in class Endpoint
    • assembleLine

      protected void assembleLine(Record record, StringBuilder lineBuffer) throws Throwable
      Description copied from class: LinedTextWriter
      Appends the record's output line, without a line terminator, to the empty buffer; also called for the header row (see AbstractWriter.isHeaderRow()).
      Specified by:
      assembleLine in class LinedTextWriter
      Throws:
      Throwable