org.dellroad.stuff.string
Class CSVOutput

java.lang.Object
  extended by org.dellroad.stuff.string.CSVOutput

public class CSVOutput
extends Object

CSV file output stream that ensures values are matched to the correct columns. This class requires the OpenCSV library.

See Also:
OpenCSV

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

CSVOutput

public CSVOutput(Writer writer,
                 String... columns)
Constructor.

Parameters:
writer - destination for the CSV output
columns - CSV columns names in their desired order
Throws:
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name

CSVOutput

public CSVOutput(Writer writer,
                 Iterable<String> columns)
Constructor.

Parameters:
writer - destination for the CSV output
columns - CSV columns names, iterated in their desired order
Throws:
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name

CSVOutput

public CSVOutput(CSVWriter writer,
                 Iterable<String> columns)
Constructor.

Parameters:
writer - CSV output object
columns - CSV columns names, iterated in their desired order
Throws:
IllegalArgumentException - if writer is null
IllegalArgumentException - if columns is null
IllegalArgumentException - if columns contains a duplicate column name
Method Detail

writeRow

public void writeRow(Map<String,?> row)
Output a CSV row.

Parameters:
row - mapping from column name to value; missing or values are treated as null
Throws:
IllegalArgumentException - if row contains an unknown column name

flush

public void flush()
           throws IOException
Flush output.

Throws:
IOException

close

public void close()
           throws IOException
Close this instance and the underlying output.

Throws:
IOException

formatObject

protected String formatObject(String columnName,
                              Object value)
Format a CSV column value.

The implementation in CSVOutput applies the following logic:

Subclasses should override as needed.

Parameters:
columnName - name of the column
value - column value; will be null if no value was present in the Map parameter to writeRow(java.util.Map)

formatDate

protected String formatDate(String columnName,
                            Date date)
Format a Date value.

The implementation in CSVOutput delegates to DateEncoder.encode().

Subclasses should override as needed.

Parameters:
columnName - name of the column
date - column value
Throws:
IllegalArgumentException - if date is null