-bottom

net.sf.csv4j
Class CSVWriter

java.lang.Object
  extended by net.sf.csv4j.CSVWriter

public class CSVWriter
extends Object

Writes output lines in CSV format.

Since:
1.0
Author:
Shawn Boyce

Constructor Summary
CSVWriter(Writer writer)
          Constructor.
CSVWriter(Writer writer, char delimiter)
          Constructor.
CSVWriter(Writer writer, char delimiter, char comment)
          Constructor.
 
Method Summary
static String escapeCSV(String text)
          Escapes a text string for CSV output.
static String escapeCSV(String text, char delimiter)
          Escapes a text string for CSV output.
static void escapeCSV(Writer writer, String text, char delimiter)
          Escapes a text string for CSV output.
 void writeCommentLine(String comment)
          Output a CSV comment line.
 void writeDataLine(SortedMap<String,String> fields)
          Output the CSV data line which contains the field values
 void writeHeaderLine(SortedMap<String,String> fields)
          Output the CSV header line which contains the names of the fields.
 void writeLine(List<String> values)
          Output a line of CSV fields.
 void writeLine(String[] values)
          Output a line of CSV fields.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVWriter

public CSVWriter(Writer writer)
Constructor. Uses default comment and delimiter characters.

Parameters:
writer - writer to output to

CSVWriter

public CSVWriter(Writer writer,
                 char delimiter)
Constructor.

Parameters:
writer - writer to output to
delimiter - field delimiter character to use

CSVWriter

public CSVWriter(Writer writer,
                 char delimiter,
                 char comment)
Constructor.

Parameters:
writer - writer to output to
delimiter - field delimiter character to use
comment - comment character to use
Method Detail

writeCommentLine

public void writeCommentLine(String comment)
                      throws IOException
Output a CSV comment line. Comment lines start with the comment character.

Example:

writer.writeCommentLine( "this is a comment" );

results in

 # this is a comment line
 

Parameters:
comment - comment text to output. Embedded newline/carriage returns are handled correctly.
Throws:
IOException - if an error occurs when writing

writeHeaderLine

public void writeHeaderLine(SortedMap<String,String> fields)
                     throws IOException
Output the CSV header line which contains the names of the fields. This method should only be called once per file.

Parameters:
fields - fields to output
Throws:
IOException - if an error occurs when writing

writeDataLine

public void writeDataLine(SortedMap<String,String> fields)
                   throws IOException
Output the CSV data line which contains the field values

Parameters:
fields - fields to output
Throws:
IOException - if an error occurs when writing

writeLine

public void writeLine(List<String> values)
               throws IOException
Output a line of CSV fields. This can be field names or values. Each field is separated by the field delimiter.

Parameters:
values - strings to output
Throws:
IOException - if an error occurs while writing

writeLine

public void writeLine(String[] values)
               throws IOException
Output a line of CSV fields. This can be field names or values. Each field is separated by the field delimiter.

Parameters:
values - strings to output
Throws:
IOException - if an error occurs while writing

escapeCSV

public static String escapeCSV(String text)
Escapes a text string for CSV output.

Parameters:
text - text string to escape
Returns:
appropriately escaped CSV text string

escapeCSV

public static String escapeCSV(String text,
                               char delimiter)
Escapes a text string for CSV output.

Parameters:
text - text string to escape
delimiter - field delimiter
Returns:
appropriately escaped CSV text string

escapeCSV

public static void escapeCSV(Writer writer,
                             String text,
                             char delimiter)
                      throws IOException
Escapes a text string for CSV output.

Parameters:
text - text string to escape
writer - writer to send the CSV output to
delimiter - field delimiter to use
Throws:
IOException - if an IO error occurs

-bottom