|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.mindprod.csv.CSVWriter
public final class CSVWriter
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.
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 |
---|
public CSVWriter(java.io.PrintWriter pw)
pw
- Buffered PrintWriter where fields will be writtenpublic CSVWriter(java.io.PrintWriter pw, int quoteLevel, char separatorChar, char quoteChar, char commentChar, boolean trim)
pw
- Buffered PrintWriter where fields will be writtenquoteLevel
- -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 |
---|
public void close()
public int getLineCount()
public void nl()
public void nl(java.lang.String comment)
comment
- comment string containing any chars. Lead comment character will be applied automatically.public void nl(java.lang.String[] fields, boolean lastFieldWasComment)
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.public void put(java.lang.String... fields)
fields
- array of strings to output.public void put(int i)
i
- The int to write. Any additional quotes or embedded quotes will be provided by put.public void put(char c)
c
- The char to write. Any additional quotes or embedded quotes will be provided by put.public void put(boolean b)
b
- The boolean to write. Any additional quotes or embedded quotes will be provided by put.public void put(long l)
l
- The long to write. Any additional quotes or embedded quotes will be provided by put.public void put(double d)
d
- The double to write. Any additional quotes or embedded quotes will be provided by put.public void put(float f)
f
- The float to write. Any additional quotes or embedded quotes will be provided by put.public void put(java.lang.String s)
s
- The string to write. Any additional quotes or embedded quotes will be provided by put. Null means start
a new line.nl(String)
public void put(double d, int places)
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.public void setLineSeparator(java.lang.String lineSeparator)
lineSeparator
- the new desired line separator String.
null gets the OS default e.g. "\n" for Unix, "\r\n" for Windows.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |