Package csv.impl

Class CSVWriter

All Implemented Interfaces:
TableWriter

public class CSVWriter
extends AbstractStreamTableWriter
Implements functionality for writing CSV streams.

Example:

java.io.File f = new java.io.File("csv-test.csv");
CSVWriter out = new CSVWriter(f);
out.printRow(new Object[] { "0:0", "0:1", "0:2" });
out.printRow(new Object[] { "1:0", "1:1", "1:2" });
out.close();
Author:
RalphSchuster
  • Constructor Details

    • CSVWriter

      public CSVWriter()
      Default constructor.
    • CSVWriter

      public CSVWriter​(java.io.File file) throws java.io.IOException
      Constructor for writing into a file.
      Parameters:
      file - file
      Throws:
      java.io.IOException - when an exception occurs
    • CSVWriter

      public CSVWriter​(java.io.OutputStream out)
      Constructor for writing into a stream.
      Parameters:
      out - output stream
    • CSVWriter

      @Deprecated public CSVWriter​(java.io.Writer out)
      Deprecated.
      For backwards compatibility only.
      Parameters:
      out - out writer
    • CSVWriter

      @Deprecated public CSVWriter​(java.io.Writer out, boolean flush)
      Deprecated.
      For backwards compatibility only.
      Parameters:
      out - out writer
      flush - has no influence and is for compatibility reasons only
    • CSVWriter

      public CSVWriter​(java.lang.String file) throws java.io.IOException
      Constructor for writing into a file.
      Parameters:
      file - file
      Throws:
      java.io.IOException - when an exception occurs
  • Method Details

    • init

      protected void init()
      Initializes the writer.
      Overrides:
      init in class AbstractTableWriter
      See Also:
      AbstractTableWriter.init()
    • getWriter

      public java.io.PrintWriter getWriter()
      Returns the underlying stream.
      Overrides:
      getWriter in class AbstractStreamTableWriter
      Returns:
      the writer object
      See Also:
      AbstractStreamTableWriter.getWriter()
    • close

      public void close()
      Closes the underlying stream.
      Specified by:
      close in interface TableWriter
      Overrides:
      close in class AbstractStreamTableWriter
      See Also:
      AbstractStreamTableWriter.close()
    • setColumnDelimiter

      public void setColumnDelimiter​(java.lang.String s)
      Sets the column delimiter to be used. Default are double-quotes. The usage of the delimiters is defined by isColumnDelimiterRequired().
      Parameters:
      s - the new delimiter
    • getColumnDelimiter

      public java.lang.String getColumnDelimiter()
      Returns the column delimiter to be used. Default are double-quotes.
      Returns:
      the column delimiter being used.
    • setColumnSeparator

      public void setColumnSeparator​(char s)
      Sets the column separator to be used. Default is semi-colon.
      Parameters:
      s - new separator character
    • getColumnSeparator

      public char getColumnSeparator()
      Returns the column separator to be used. Default is semi-colon.
      Returns:
      the column separator character being used
    • setRowSeparator

      public void setRowSeparator​(java.lang.String s)
      Sets the row separator to be used. Actually you should never change this character as it will break the definition of a CSV stream. Default is newline character.
      Parameters:
      s - new separator
    • getRowSeparator

      public java.lang.String getRowSeparator()
      Returns the row separator to be used. Default is a newline character.
      Returns:
      the row separator character.
    • setColumnDelimiterRequired

      public void setColumnDelimiterRequired​(boolean b)
      Sets if column separators are always required or not. Usually, column delimiters are written only when the column value contains special characters, such as the delimiters for columns and rows. Default value is false.
      Parameters:
      b - true when delimiters shall always be written.
    • isColumnDelimiterRequired

      public boolean isColumnDelimiterRequired()
      Returns whether column delimiters are always required. Default value is false.
      Returns:
      true if column delimiters are written on each column.
    • getCommentChar

      public char getCommentChar()
      Returns the character used for indicating comments
      Returns:
      the commentChar
    • setCommentChar

      public void setCommentChar​(char commentChar)
      Sets the character to be used for indicating comments
      Parameters:
      commentChar - the commentChar to set
    • prepareRow

      public java.lang.String prepareRow​(java.lang.Object[] columns)
      Formats a row for CSV output.
      Parameters:
      columns - columns to be prepared
      Returns:
      string ready to be printed in CSV
    • printRow

      public void printRow​(java.lang.Object[] columns) throws java.io.IOException
      Prints a new row into the CSV file. This is the method where an actual CSV row will be printed. The columns are prepared to follow the CSV syntax rules and definitions. The underlying stream in flushed immediately.
      Parameters:
      columns - array of column values.
      Throws:
      java.io.IOException - when an exception occurs
    • prepareComment

      public java.lang.String prepareComment​(java.lang.String comment)
      Formats a comment for printing
      Parameters:
      comment - comment to be printed
      Returns:
      string ready to be written to CSV
    • printComment

      public void printComment​(java.lang.String comment) throws java.io.IOException
      Prints a comment into the CSV stream.
      Specified by:
      printComment in interface TableWriter
      Overrides:
      printComment in class AbstractTableWriter
      Parameters:
      comment - comment to write
      Throws:
      java.io.IOException - when an exception occurs
    • printComment

      public void printComment​(java.lang.String comment, int row, int column) throws java.io.IOException
      Prints a comment into the stream. Note that not all implementations support comments.
      Specified by:
      printComment in interface TableWriter
      Overrides:
      printComment in class AbstractTableWriter
      Parameters:
      comment - comment to write
      row - index of row for comment
      column - index of column for comment
      Throws:
      java.io.IOException - when an exception occurs
    • columnNeedsDelimiting

      protected boolean columnNeedsDelimiting​(java.lang.String s)
      Decides if a column value needs to be wrapped with delimiters.
      Parameters:
      s - column value to check
      Returns:
      true if value must be wrapped in output
    • prepareColumnValue

      protected java.lang.String prepareColumnValue​(java.lang.String s)
      Replaces all occurrences of the delimiter by doubling it.
      Parameters:
      s - column value to parse
      Returns:
      value with replaced delimiters.
    • getColumnCount

      public int getColumnCount()
      Returns the columns written to the stream.
      Returns:
      the columnCount