|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.dellroad.stuff.string.CSVOutput
public class CSVOutput
CSV file output stream that ensures values are matched to the correct columns. This class requires the OpenCSV library.
| Constructor Summary | |
|---|---|
CSVOutput(CSVWriter writer,
Iterable<String> columns)
Constructor. |
|
CSVOutput(Writer writer,
Iterable<String> columns)
Constructor. |
|
CSVOutput(Writer writer,
String... columns)
Constructor. |
|
| Method Summary | |
|---|---|
void |
close()
Close this instance and the underlying output. |
void |
flush()
Flush output. |
protected String |
formatDate(String columnName,
Date date)
Format a Date value. |
protected String |
formatObject(String columnName,
Object value)
Format a CSV column value. |
void |
writeRow(Map<String,?> row)
Output a CSV row. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public CSVOutput(Writer writer,
String... columns)
writer - destination for the CSV outputcolumns - CSV columns names in their desired order
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name
public CSVOutput(Writer writer,
Iterable<String> columns)
writer - destination for the CSV outputcolumns - CSV columns names, iterated in their desired order
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name
public CSVOutput(CSVWriter writer,
Iterable<String> columns)
writer - CSV output objectcolumns - CSV columns names, iterated in their desired order
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name| Method Detail |
|---|
public void writeRow(Map<String,?> row)
row - mapping from column name to value; missing or values are treated as null
IllegalArgumentException - if row contains an unknown column name
public void flush()
throws IOException
IOException
public void close()
throws IOException
IOException
protected String formatObject(String columnName,
Object value)
The implementation in CSVOutput applies the following logic:
null values are output as the empty stringBoolean values are output as 1 or 0Date values are output by delegating to formatDate()String.valueOf(java.lang.Object)Subclasses should override as needed.
columnName - name of the columnvalue - column value; will be null if no value was present in the Map parameter to writeRow(java.util.Map)
protected String formatDate(String columnName,
Date date)
Date value.
The implementation in CSVOutput delegates to DateEncoder.encode().
Subclasses should override as needed.
columnName - name of the columndate - column value
IllegalArgumentException - if date is null
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||