Class CsvWriter

java.lang.Object
org.jumpmind.symmetric.csv.CsvWriter
All Implemented Interfaces:
AutoCloseable

public class CsvWriter extends Object implements AutoCloseable
A stream based writer for writing delimited text data to a file or a stream.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Use a backslash character before the text qualifier to represent an occurrence of the text qualifier.
    static final int
    Double up the text qualifier to represent an occurrence of the text qualifier.
  • Constructor Summary

    Constructors
    Constructor
    Description
    CsvWriter(OutputStream outputStream, char delimiter, Charset charset)
    Creates a CsvWriter object using an OutputStream to write data to.
    CsvWriter(Writer outputStream, char delimiter)
    Creates a CsvWriter object using a Writer to write data to.
    CsvWriter(String fileName)
    Creates a CsvWriter object using a file as the data destination. Uses a comma as the column delimiter and ISO-8859-1 as the Charset.
    CsvWriter(String fileName, char delimiter, Charset charset)
    Creates a CsvWriter object using a file as the data destination.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes and releases all related resources.
    void
    Ends the current record by sending the record delimiter.
    void
    Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
    char
     
    char
    Gets the character being used as the column delimiter.
    int
     
    boolean
    Whether fields will be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.
     
    char
     
    char
    Gets the character to use as a text qualifier in the data.
    boolean
    Whether text qualifiers will be used while writing data or not.
    static String
    replace(String original, String pattern, String replace)
     
    void
    setComment(char comment)
     
    void
    setDelimiter(char delimiter)
    Sets the character to use as the column delimiter.
    void
    setEscapeMode(int escapeMode)
     
    void
    setForceQualifier(boolean forceQualifier)
    Use this to force all fields to be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.
    void
    setNullString(String nullString)
     
    void
    setRecordDelimiter(char recordDelimiter)
    Sets the character to use as the record delimiter.
    void
    setTextQualifier(char textQualifier)
    Sets the character to use as a text qualifier in the data.
    void
    setUseTextQualifier(boolean useTextQualifier)
    Sets whether text qualifiers will be used while writing data or not.
    void
    write(String content)
    Writes another column of data to this record. Does not preserve leading and trailing whitespace in this column of data.
    void
    write(String content, boolean preserveSpaces)
    Writes another column of data to this record.
    void
    writeComment(String commentText)
     
    void
    writeRecord(String[] values)
    Writes a new record using the passed in array of values.
    void
    writeRecord(String[] values, boolean preserveSpaces)
    Writes a new record using the passed in array of values.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • ESCAPE_MODE_DOUBLED

      public static final int ESCAPE_MODE_DOUBLED
      Double up the text qualifier to represent an occurrence of the text qualifier.
      See Also:
    • ESCAPE_MODE_BACKSLASH

      public static final int ESCAPE_MODE_BACKSLASH
      Use a backslash character before the text qualifier to represent an occurrence of the text qualifier.
      See Also:
  • Constructor Details

    • CsvWriter

      public CsvWriter(String fileName, char delimiter, Charset charset)
      Creates a CsvWriter object using a file as the data destination.
      Parameters:
      fileName - The path to the file to output the data.
      delimiter - The character to use as the column delimiter.
      charset - The Charset to use while writing the data.
    • CsvWriter

      public CsvWriter(String fileName)
      Creates a CsvWriter object using a file as the data destination. Uses a comma as the column delimiter and ISO-8859-1 as the Charset.
      Parameters:
      fileName - The path to the file to output the data.
    • CsvWriter

      public CsvWriter(Writer outputStream, char delimiter)
      Creates a CsvWriter object using a Writer to write data to.
      Parameters:
      outputStream - The stream to write the column delimited data to.
      delimiter - The character to use as the column delimiter.
    • CsvWriter

      public CsvWriter(OutputStream outputStream, char delimiter, Charset charset)
      Creates a CsvWriter object using an OutputStream to write data to.
      Parameters:
      outputStream - The stream to write the column delimited data to.
      delimiter - The character to use as the column delimiter.
      charset - The Charset to use while writing the data.
  • Method Details

    • getDelimiter

      public char getDelimiter()
      Gets the character being used as the column delimiter.
      Returns:
      The character being used as the column delimiter.
    • setDelimiter

      public void setDelimiter(char delimiter)
      Sets the character to use as the column delimiter.
      Parameters:
      delimiter - The character to use as the column delimiter.
    • getRecordDelimiter

      public char getRecordDelimiter()
    • setRecordDelimiter

      public void setRecordDelimiter(char recordDelimiter)
      Sets the character to use as the record delimiter.
      Parameters:
      recordDelimiter - The character to use as the record delimiter. Default is combination of standard end of line characters for Windows, Unix, or Mac.
    • getTextQualifier

      public char getTextQualifier()
      Gets the character to use as a text qualifier in the data.
      Returns:
      The character to use as a text qualifier in the data.
    • setTextQualifier

      public void setTextQualifier(char textQualifier)
      Sets the character to use as a text qualifier in the data.
      Parameters:
      textQualifier - The character to use as a text qualifier in the data.
    • getUseTextQualifier

      public boolean getUseTextQualifier()
      Whether text qualifiers will be used while writing data or not.
      Returns:
      Whether text qualifiers will be used while writing data or not.
    • setUseTextQualifier

      public void setUseTextQualifier(boolean useTextQualifier)
      Sets whether text qualifiers will be used while writing data or not.
      Parameters:
      useTextQualifier - Whether to use a text qualifier while writing data or not.
    • getEscapeMode

      public int getEscapeMode()
    • setEscapeMode

      public void setEscapeMode(int escapeMode)
    • setComment

      public void setComment(char comment)
    • getComment

      public char getComment()
    • getForceQualifier

      public boolean getForceQualifier()
      Whether fields will be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field.
      Returns:
      Whether fields will be forced to be qualified or not.
    • setForceQualifier

      public void setForceQualifier(boolean forceQualifier)
      Use this to force all fields to be surrounded by the text qualifier even if the qualifier is not necessarily needed to escape this field. Default is false.
      Parameters:
      forceQualifier - Whether to force the fields to be qualified or not.
    • getNullString

      public String getNullString()
    • setNullString

      public void setNullString(String nullString)
    • write

      public void write(String content, boolean preserveSpaces) throws IOException
      Writes another column of data to this record.
      Parameters:
      content - The data for the new column.
      preserveSpaces - Whether to preserve leading and trailing whitespace in this column of data.
      Throws:
      IOException - Thrown if an error occurs while writing data to the destination stream.
    • write

      public void write(String content) throws IOException
      Writes another column of data to this record. Does not preserve leading and trailing whitespace in this column of data.
      Parameters:
      content - The data for the new column.
      Throws:
      IOException - Thrown if an error occurs while writing data to the destination stream.
    • writeComment

      public void writeComment(String commentText) throws IOException
      Throws:
      IOException
    • writeRecord

      public void writeRecord(String[] values, boolean preserveSpaces) throws IOException
      Writes a new record using the passed in array of values.
      Parameters:
      values - Values to be written.
      preserveSpaces - Whether to preserver leading and trailing spaces in columns while writing out to the record or not.
      Throws:
      IOException - Thrown if an error occurs while writing data to the destination stream.
    • writeRecord

      public void writeRecord(String[] values) throws IOException
      Writes a new record using the passed in array of values.
      Parameters:
      values - Values to be written.
      Throws:
      IOException - Thrown if an error occurs while writing data to the destination stream.
    • endRecord

      public void endRecord() throws IOException
      Ends the current record by sending the record delimiter.
      Throws:
      IOException - Thrown if an error occurs while writing data to the destination stream.
    • flush

      public void flush()
      Clears all buffers for the current writer and causes any buffered data to be written to the underlying device.
    • close

      public void close()
      Closes and releases all related resources.
      Specified by:
      close in interface AutoCloseable
    • replace

      public static String replace(String original, String pattern, String replace)