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(File file) throws IOException
      Constructor for writing into a file.
      Parameters:
      file - file
      Throws:
      IOException - when an exception occurs
    • CSVWriter

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

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

    • init

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

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

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

      public void setColumnDelimiter(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 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(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 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 String prepareRow(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(Object[] columns) throws 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:
      IOException - when an exception occurs
    • prepareComment

      public String prepareComment(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(String comment) throws 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:
      IOException - when an exception occurs
    • printComment

      public void printComment(String comment, int row, int column) throws 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:
      IOException - when an exception occurs
    • columnNeedsDelimiting

      protected boolean columnNeedsDelimiting(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 String prepareColumnValue(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