com.mindprod.csv
Class CSVWriter

java.lang.Object
  extended by com.mindprod.csv.CSVWriter

public final class CSVWriter
extends java.lang.Object

Write CSV (Comma Separated Value) files.

This format is used my Microsoft Word and Excel. Fields are separated by commas, and enclosed in quotes if they contain commas or quotes. Embedded quotes are doubled. Embedded spaces do not normally require surrounding quotes. The last field on the line is not followed by a comma. Null fields are represented by two commas in a row.

Must be combined with your own code or used by one of the standalone CSV utilities.

Since:
2002-03-27
Version:
4.6 2011-02-24 fix bug, emitted """" for single field with quotelevel 2. reported by Dr. Jens Uwe Meyborn
Author:
Roedy Green, Canadian Mind Products

Constructor Summary
CSVWriter(java.io.PrintWriter pw)
          Simplified convenience Constructor to write a CSV file, defaults to quotelevel 1, comma separator , trim
CSVWriter(java.io.PrintWriter pw, int quoteLevel, char separatorChar, char quoteChar, char commentChar, boolean trim)
          Detailed constructor to write a CSV file.
 
Method Summary
 void close()
          Close the PrintWriter.
 int getLineCount()
          get count of how many lines written so far.
 void nl()
          Write a new line in the CVS output file to demark the end of record.
 void nl(java.lang.String comment)
          Write a comment followed by new line in the CVS output file to demark the end of record.
 void nl(java.lang.String[] fields, boolean lastFieldWasComment)
          Write a comment followed by new line in the CVS output file to demark the end of record.
 void put(boolean b)
          Write one boolean field to the file, followed by a separator unless it is the last field on the line.
 void put(char c)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(double d)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(double d, int places)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(float f)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(int i)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(long l)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void put(java.lang.String... fields)
          Write a variable number of Strings
 void put(java.lang.String s)
          Write one csv field to the file, followed by a separator unless it is the last field on the line.
 void setLineSeparator(java.lang.String lineSeparator)
          Set the line separator used to demark where one line ends and the next begins.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CSVWriter

public CSVWriter(java.io.PrintWriter pw)
Simplified convenience Constructor to write a CSV file, defaults to quotelevel 1, comma separator , trim

Parameters:
pw - Buffered PrintWriter where fields will be written

CSVWriter

public CSVWriter(java.io.PrintWriter pw,
                 int quoteLevel,
                 char separatorChar,
                 char quoteChar,
                 char commentChar,
                 boolean trim)
Detailed constructor to write a CSV file.

Parameters:
pw - Buffered PrintWriter where fields will be written
quoteLevel - -1 = like 0, but add an extra space after each separator/comma, 0 = minimal quotes, only around fields containing quotes or separators. 1 = quotes also around fields containing spaces. 2 = quotes around all fields, whether or not they contain commas, quotes or spaces.
separatorChar - field separator character, usually ',' in North America, ';' in Europe and sometimes '\t' for tab. Note this is a 'char' not a "string".
quoteChar - char to use to enclose fields containing a separator, usually '\"'. Use (char)0 if you don't want a quote character. Note this is a 'char' not a "string".
commentChar - char to prepend on any comments you write. usually ; or #. Note this is a 'char' not a "string".
trim - true if writer should trim leading/trailing whitespace (e.g. blank, cr, Lf, tab) before writing the field.
Method Detail

close

public void close()
Close the PrintWriter.


getLineCount

public int getLineCount()
get count of how many lines written so far.

Returns:
count of lines written so far

nl

public void nl()
Write a new line in the CVS output file to demark the end of record.


nl

public void nl(java.lang.String comment)
Write a comment followed by new line in the CVS output file to demark the end of record.

Parameters:
comment - comment string containing any chars. Lead comment character will be applied automatically.

nl

public void nl(java.lang.String[] fields,
               boolean lastFieldWasComment)
Write a comment followed by new line in the CVS output file to demark the end of record.

Parameters:
fields - array of strings to output. Last field may be a comment. Typically from getAllFieldsInLine.
lastFieldWasComment - if true, mean last field in the array was a comment.

put

public void put(java.lang.String... fields)
Write a variable number of Strings

Parameters:
fields - array of strings to output.

put

public void put(int i)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
i - The int to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(char c)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
c - The char to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(boolean b)
Write one boolean field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
b - The boolean to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(long l)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
l - The long to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(double d)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
d - The double to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(float f)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
f - The float to write. Any additional quotes or embedded quotes will be provided by put.

put

public void put(java.lang.String s)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed. Don't use this method to write comments.

Parameters:
s - The string to write. Any additional quotes or embedded quotes will be provided by put. Null means start a new line.
See Also:
nl(String)

put

public void put(double d,
                int places)
Write one csv field to the file, followed by a separator unless it is the last field on the line. Lead and trailing blanks will be removed.

Parameters:
d - The double to write. Any additional quotes or embedded quotes will be provided by put.
places - lets you explicitly control how max places past the decimal to output.

setLineSeparator

public void setLineSeparator(java.lang.String lineSeparator)
Set the line separator used to demark where one line ends and the next begins. The default is "\r\n" because CSV is a Microsoft format.

Note the spelling separator not seperator.

Parameters:
lineSeparator - the new desired line separator String. null gets the OS default e.g. "\n" for Unix, "\r\n" for Windows.