com.screenscraper.csv
Class CsvWriter

java.lang.Object
  extended by com.screenscraper.csv.CsvWriter

public class CsvWriter
extends java.lang.Object

Basic Usage:
CsvWriter writer = new CsvWriter( "somefile.csv" );
writer.setHeaderRow( String[] );
writer.writeNext( String[] );
writer.close();


Constructor Summary
CsvWriter(java.lang.String filePath)
          Constructs CSVWriter with the given filePath as the output file
CsvWriter(java.lang.String filePath, boolean addTimeStamp)
          Constructs CSVWriter with the given filePath If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')
CsvWriter(java.lang.String filePath, char separator)
          Constructs CSVWriter with the given filePath and separator.
CsvWriter(java.lang.String filePath, char separator, boolean addTimeStamp)
          Constructs CSVWriter with the given filePath and separator If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')
CsvWriter(java.lang.String filePath, char separator, char quotechar)
          Constructs CSVWriter with the given filePath, separator and quotchar
CsvWriter(java.lang.String filePath, char separator, char quotechar, char escapechar)
          Constructs CSVWriter with the given filePath, separator, quote character and escape character
CsvWriter(java.lang.String filePath, char separator, char quotechar, char escapechar, java.lang.String lineEnd)
          Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
CsvWriter(java.lang.String filePath, char separator, char quotechar, java.lang.String lineEnd)
          Constructs CSVWriter with the given filePath, separator, quote character, escape character and line end character
 
Method Summary
 void close()
          Close the underlying stream writer flushing any buffered content.
 void flush()
          Flush underlying stream to writer.
 java.lang.String getCsvPath()
          Returns the path of the file being written to.
 java.lang.String[] getHeaderRow()
          Returns the header row
 java.lang.String[] getMappingLabels()
          Returns the mapping labels
 void setHeader(java.lang.String[] header)
          Sets the header row to the given String[] and also sets mapping Values to the same String[] The header is only written if the file doesn't originally exist when writing to it.
 void setMapValues(java.lang.String[] mapVals)
          Set the values used to map the key values of a Map into CSV rows.
 void setMapValuesAndLowercaseUnderscore(java.lang.String[] mapVals)
          Set the mapping values with the given String[], but convert them to lowercase and underscores for spaces eg.
 void setMapValuesAndUnderscore(java.lang.String[] mapVals)
          Set the mapping values with the given String[], but convert them to have underscores for spaces eg.
 void setMapValuesAndUppercaseUnderscore(java.lang.String[] mapVals)
          Set the mapping values with the given String[], but convert them to uppercase and underscores for spaces eg.
 void write(java.util.List nextLine)
          Writes the next line to the file.
 void write(java.util.Map nextLine)
          Writes the next line to the file.
 void write(java.lang.String[] nextLine)
          Writes the next line to the file.
 void writeAll(DataSet set)
          Writes all the dataRecords in the set to the CSV (The map values must be set for this to work)
 void writeAll(java.util.List lines)
          Writes the entire list to a CSV file.
 void writeAll(java.sql.ResultSet rs, boolean includeColumnNames)
          Writes the entire ResultSet to a CSV file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CsvWriter

public CsvWriter(java.lang.String filePath)
          throws java.io.IOException
Constructs CSVWriter with the given filePath as the output file

Parameters:
filePath - the filepath for the underlying CSV source.
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator)
          throws java.io.IOException
Constructs CSVWriter with the given filePath and separator.

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries.
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 boolean addTimeStamp)
          throws java.io.IOException
Constructs CSVWriter with the given filePath If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')

Parameters:
filePath - the filepath for the underlying CSV source.
addTimeStamp - whether to add a current time stamp to the filename
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator,
                 boolean addTimeStamp)
          throws java.io.IOException
Constructs CSVWriter with the given filePath and separator If the timestamp is set to true, a timestamp will be added to the given filename (yyyy-MM-dd'T'hh'H'mm'M'ss'S')

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries.
addTimeStamp - whether to add a current time stamp to the filename
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator,
                 char quotechar)
          throws java.io.IOException
Constructs CSVWriter with the given filePath, separator and quotchar

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator,
                 char quotechar,
                 char escapechar)
          throws java.io.IOException
Constructs CSVWriter with the given filePath, separator, quote character and escape character

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escapechar - the character to use for escaping quotechars or escapechars
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator,
                 char quotechar,
                 java.lang.String lineEnd)
          throws java.io.IOException
Constructs CSVWriter with the given filePath, separator, quote character, escape character and line end character

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
lineEnd - the line feed terminator to use
Throws:
java.io.IOException

CsvWriter

public CsvWriter(java.lang.String filePath,
                 char separator,
                 char quotechar,
                 char escapechar,
                 java.lang.String lineEnd)
          throws java.io.IOException
Constructs CSVWriter with supplied separator, quote char, escape char and line ending.

Parameters:
filePath - the filepath for the underlying CSV source.
separator - the delimiter to use for separating entries
quotechar - the character to use for quoted elements
escapechar - the character to use for escaping quotechars or escapechars
lineEnd - the line feed terminator to use
Throws:
java.io.IOException
Method Detail

setHeader

public void setHeader(java.lang.String[] header)
Sets the header row to the given String[] and also sets mapping Values to the same String[] The header is only written if the file doesn't originally exist when writing to it. The default mapping values are the result of calling setMapValuesAndUppercaseUnderscore

Parameters:
header -

setMapValues

public void setMapValues(java.lang.String[] mapVals)
Set the values used to map the key values of a Map into CSV rows.

Parameters:
mapVals -

setMapValuesAndUppercaseUnderscore

public void setMapValuesAndUppercaseUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to uppercase and underscores for spaces eg. {"A key", "KEY 2"} becomes the mapping values of {"A_KEY", "KEY_2"}

Parameters:
mapVals -

setMapValuesAndLowercaseUnderscore

public void setMapValuesAndLowercaseUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to lowercase and underscores for spaces eg. {"A key", "KEY 2"} becomes the mapping values of {"a_key", "key_2"}

Parameters:
mapVals -

setMapValuesAndUnderscore

public void setMapValuesAndUnderscore(java.lang.String[] mapVals)
Set the mapping values with the given String[], but convert them to have underscores for spaces eg. {"A key", "KEY 2"} becomes the mapping values of {"A_key", "KEY_2"}

Parameters:
mapVals -

writeAll

public void writeAll(java.util.List lines)
Writes the entire list to a CSV file. The list is assumed contain String[] or List or List>

Parameters:
allLines - a List of String[] or List, each item representing a line of the file.

writeAll

public void writeAll(DataSet set)
Writes all the dataRecords in the set to the CSV (The map values must be set for this to work)

Parameters:
set -

write

public void write(java.util.List nextLine)
Writes the next line to the file.

Parameters:
nextLine - a list of Strings with each comma-separated element as a separate entry.

write

public void write(java.util.Map nextLine)
Writes the next line to the file. (The map values must be set for this to work) Takes a map where the key is mapped to a header row name.

Parameters:
nextLine - a map of String => String

write

public void write(java.lang.String[] nextLine)
Writes the next line to the file.

Parameters:
nextLine - a string array with each comma-separated element as a separate entry.

flush

public void flush()
           throws java.io.IOException
Flush underlying stream to writer.

Throws:
java.io.IOException - if bad things happen

close

public void close()
           throws java.io.IOException
Close the underlying stream writer flushing any buffered content.

Throws:
java.io.IOException - if bad things happen

writeAll

public void writeAll(java.sql.ResultSet rs,
                     boolean includeColumnNames)
              throws java.sql.SQLException,
                     java.io.IOException
Writes the entire ResultSet to a CSV file.

The caller is responsible for closing the ResultSet.

Parameters:
rs - the recordset to write
includeColumnNames - true if you want column names in the output, false otherwise
Throws:
java.sql.SQLException
java.io.IOException

getCsvPath

public java.lang.String getCsvPath()
Returns the path of the file being written to.

Returns:

getHeaderRow

public java.lang.String[] getHeaderRow()
Returns the header row

Returns:

getMappingLabels

public java.lang.String[] getMappingLabels()
Returns the mapping labels

Returns: