Package de.mhus.lib.core.io
Class CSVWriter
- java.lang.Object
-
- de.mhus.lib.core.io.CSVWriter
-
public class CSVWriter extends 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.- Author:
- copyright (c) 2002-2006 Roedy Green Canadian Mind Products version
1.0 2002 March 27
1.1 2002 March 28 - allow variable separator - add close method
1.2 2002 April 23 - put in to separate package
1.3 2002 April 24 - three levels of quoting
1.4 2002 April 24 - convenience constructor - put(null) now means nl.
1.6 2002 May 25 - allow choice of quote char
1.9 2002 November 14 - trim parameter to control whether fields are trimmed of lead/trail whitespace (blanks, Cr, Lf, Tab etc.) before writing.
2.1 2005-07-17 reorganization, new bat files.
2.2 2005-08-28 - add CSVAlign and CSVPack to the suite. 2.3 2015-06-12 fix enclosing of fields
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCSVWriter.DefaultValueWriterstatic interfaceCSVWriter.ValueWriter
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close the PrintWriter.intgetColumnOfLine()StringgetDefaultNullValue()StringgetLineSeparator()PrintWritergetPrintWriter()chargetQuote()intgetQuoteLevel()CSVWriter.ValueWritergetValueWriter()booleanisTrim()voidnl()Write a new line in the CVS output file to demark the end of record.voidprint(Object... values)voidprintln(Object... values)voidput(String... values)Write one csv field to the file, followed by a separator unless it is the last field on the line.voidsetDefaultNullValue(String in)Set default value if put() gets a value of null.voidsetLineSeparator(String lineSeparator)voidsetValueWriter(CSVWriter.ValueWriter writer)
-
-
-
Constructor Detail
-
CSVWriter
public CSVWriter(Writer pw)
convenience Constructor, defaults to quotelevel 1, comma separator , trim- Parameters:
pw- Writer where fields will be written.
-
CSVWriter
public CSVWriter(Writer pw, int quoteLevel, char separator, char quote, boolean trim)
Constructor- Parameters:
pw- Writer where fields will be written.quoteLevel- 0 = minimal quotes
1 = quotes also around fields containing spaces
2 = quotes around all fields, whether or not they contain commas, quotes or spaces.separator- field separator character, usually ',' in North America, ';' in Europe and sometimes '\t' for tab.quote- char to use to enclose fields containing a separator, usually '\"'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.
-
setDefaultNullValue
public void setDefaultNullValue(String in)
Set default value if put() gets a value of null. If the defaultNullValue is also null put() will write a new line. Default is a empty string.- Parameters:
in-
-
getDefaultNullValue
public String getDefaultNullValue()
-
nl
public void nl()
Write a new line in the CVS output file to demark the end of record.
-
print
public void print(Object... values)
-
println
public void println(Object... values)
-
put
public void put(String... values)
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:
values-
-
getLineSeparator
public String getLineSeparator()
-
setLineSeparator
public void setLineSeparator(String lineSeparator)
-
isTrim
public boolean isTrim()
-
getQuote
public char getQuote()
-
getPrintWriter
public PrintWriter getPrintWriter()
-
getQuoteLevel
public int getQuoteLevel()
-
getColumnOfLine
public int getColumnOfLine()
-
getValueWriter
public CSVWriter.ValueWriter getValueWriter()
-
setValueWriter
public void setValueWriter(CSVWriter.ValueWriter writer)
-
-