Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

csv.impl
Class CSVWriter

java.lang.Object
  extended by csv.impl.AbstractTableWriter
      extended by csv.impl.AbstractStreamTableWriter
          extended by csv.impl.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 Summary
CSVWriter()
          Default constructor.
CSVWriter(java.io.File file)
          Constructor for writing into a file.
CSVWriter(java.io.OutputStream out)
          Constructor for writing into a stream.
CSVWriter(java.lang.String file)
          Constructor for writing into a file.
CSVWriter(java.io.Writer out)
          Deprecated. Use CSVWriter(OutputStream) instead.
CSVWriter(java.io.Writer out, boolean flush)
          Deprecated.  
 
Method Summary
 void close()
          Closes the underlying stream.
protected  boolean columnNeedsDelimiting(java.lang.String s)
          Decides if a column value needs to be wrapped with delimiters.
 int getColumnCount()
          Returns the columns written to the stream.
 java.lang.String getColumnDelimiter()
          Returns the column delimiter to be used.
 char getColumnSeparator()
          Returns the column separator to be used.
 char getCommentChar()
          Returns the character used for indicating comments
 java.lang.String getRowSeparator()
          Returns the row separator to be used.
 java.io.PrintWriter getWriter()
          Returns the underlying stream.
protected  void init()
          Initializes the writer.
 boolean isColumnDelimiterRequired()
          Returns whether column delimiters are always required.
protected  java.lang.String prepareColumnValue(java.lang.String s)
          Replaces all occurrences of the delimiter by doubling it.
 java.lang.String prepareComment(java.lang.String comment)
          Formats a comment for printing
 java.lang.String prepareRow(java.lang.Object[] columns)
          Formats a row for CSV output.
 void printComment(java.lang.String comment)
          Prints a comment into the CSV stream.
 void printComment(java.lang.String comment, int row, int column)
          Prints a comment into the stream.
 void printRow(java.util.Collection<?> columns)
          Prints a single row into the CSV stream.
 void printRow(java.util.Iterator<?> columns)
          Prints a new row into the CSV stream.
 void printRow(java.util.Iterator<?> columns, int size)
          Prints a single row into the CSV stream.
 void printRow(java.lang.Object[] columns)
          Prints a new row into the CSV file.
 void setColumnDelimiter(java.lang.String s)
          Sets the column delimiter to be used.
 void setColumnDelimiterRequired(boolean b)
          Sets if column separators are always required or not.
 void setColumnSeparator(char s)
          Sets the column separator to be used.
 void setCommentChar(char commentChar)
          Sets the character to be used for indicating comments
 void setRowSeparator(java.lang.String s)
          Sets the row separator to be used.
 
Methods inherited from class csv.impl.AbstractStreamTableWriter
getOutputStream, setOutputStream
 
Methods inherited from class csv.impl.AbstractTableWriter
convert, convert, getRowCount, getTypeConversionHandler, incrementRowCount, registerTypeConversionHandler, unregisterTypeConversionHandler
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

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
Throws:
java.io.IOException - when an exception occurs

CSVWriter

public CSVWriter(java.io.Writer out)
Deprecated. Use CSVWriter(OutputStream) instead.

For backwards compatibility only.

Parameters:
out - out writer

CSVWriter

public CSVWriter(java.io.Writer out,
                 boolean flush)
Deprecated. 

For backwards compatibility only.

Parameters:
out - out writer

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 Detail

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.

printRow

public void printRow(java.util.Collection<?> columns)
              throws java.io.IOException
Prints a single row into the CSV stream. The columns are written to the CSV stream as the are delivered by the collection's iterator.

Parameters:
columns - collection of column values. An iterator will be used to retrieve values from the collection.
Throws:
java.io.IOException

printRow

public void printRow(java.util.Iterator<?> columns,
                     int size)
              throws java.io.IOException
Prints a single row into the CSV stream. The columns are written to the CSV stream as delivered by the iterator.

Parameters:
columns - iterator that returns column values.
size - number of values to retrieve from iterator. Method will abort at this size.
Throws:
java.io.IOException

printRow

public void printRow(java.util.Iterator<?> columns)
              throws java.io.IOException
Prints a new row into the CSV stream. The columns are written to the CSV stream as delivered by the iterator.

Parameters:
columns - iterator that returns column values.
Throws:
java.io.IOException

getColumnCount

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

Returns:
the columnCount

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.

Copyright © 2008-2011 Ralph Schuster. All Rights Reserved.