Jive Forums API (5.5.20.2-oracle) Developer Javadocs

com.jivesoftware.base.stats.output
Class CSVWriter

java.lang.Object
  extended by java.io.Writer
      extended by com.jivesoftware.base.stats.output.CSVWriter
All Implemented Interfaces:
java.io.Closeable, java.io.Flushable, java.lang.Appendable

public class CSVWriter
extends java.io.Writer

A writer which outputs DataTable data as CSV (comma-separated values) output.

Sample usage:

CSVWriter writer = new CVSWriter(new FileWriter(new File("out.csv")));
DataTable table = // get a data table
writer.write(table);

Using the writeTitle(String), writeColumns(String[]), writeRow(Object[]) methods you can create CSV output without using a DataTable.

Finally, there is a convience method to escape a given String for a CSV file (removes commas). See the escapeForCSV(String) method for more details.

See Also:
DataTable

Field Summary
 
Fields inherited from class java.io.Writer
lock
 
Constructor Summary
CSVWriter(java.io.Writer writer)
          Creates a new CSV writer given a writer.
 
Method Summary
 void close()
           
static java.lang.String escapeForCSV(java.lang.String str)
          Escapes a given string for CSV output by removing commas with a blank character.
 void flush()
           
 void write(char[] cbuf, int off, int len)
           
 void writeColumns(java.lang.String[] columns)
          Writes an array of columns out in CSV format.
 void writeCSV(DataTable table)
          Writes the entire table out in CSV format.
 void writeRow(java.lang.Object[] row)
          Writes a row out as CSV data.
 void writeTitle(java.lang.String title)
          Write just the title of the CSV file.
 
Methods inherited from class java.io.Writer
append, append, append, write, write, write, write
 
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.Writer writer)
Creates a new CSV writer given a writer.

Parameters:
writer - the writer to write CSV output to.
Method Detail

write

public void write(char[] cbuf,
                  int off,
                  int len)
           throws java.io.IOException
Specified by:
write in class java.io.Writer
Throws:
java.io.IOException

flush

public void flush()
           throws java.io.IOException
Specified by:
flush in interface java.io.Flushable
Specified by:
flush in class java.io.Writer
Throws:
java.io.IOException

close

public void close()
           throws java.io.IOException
Specified by:
close in interface java.io.Closeable
Specified by:
close in class java.io.Writer
Throws:
java.io.IOException

writeCSV

public void writeCSV(DataTable table)
              throws java.io.IOException
Writes the entire table out in CSV format.

Parameters:
table - the data table to write.
Throws:
java.io.IOException - if an exception occurs when writing the data.

writeTitle

public void writeTitle(java.lang.String title)
                throws java.io.IOException
Write just the title of the CSV file.

Parameters:
title - the title of the
Throws:
java.io.IOException

writeColumns

public void writeColumns(java.lang.String[] columns)
                  throws java.io.IOException
Writes an array of columns out in CSV format.

Parameters:
columns - an array of column names.
Throws:
java.io.IOException - if an exception occurs when writing the columns.

writeRow

public void writeRow(java.lang.Object[] row)
              throws java.io.IOException
Writes a row out as CSV data. For each item in the array, .toString() will be called.

Parameters:
row - the row data.
Throws:
java.io.IOException - if an exception occurs when writing out the data.

escapeForCSV

public static java.lang.String escapeForCSV(java.lang.String str)
Escapes a given string for CSV output by removing commas with a blank character.

Parameters:
str - the string to escape.
Returns:
a string with commas replaced as blank strings.

Jive Forums Project Page

Copyright © 1999-2006 Jive Software.