de.hpi.fgis.dude.util.csv
Class CSVWriter

java.lang.Object
  extended by de.hpi.fgis.dude.util.csv.CSVWriter

public class CSVWriter
extends Object

CSVWriter generates CSV formatted data.

Author:
Matthias Pohl

Field Summary
static char DEFAULT_ESCAPE_CHARACTER
          The default escape character.
static char DEFAULT_QUOTE_CHARACTER
          The default quote character.
static char DEFAULT_SEPARATOR
          The default separator character.
 
Constructor Summary
CSVWriter(OutputStream stream)
          Initializes a CSVWriter with the given OutputStream.
CSVWriter(Writer writer)
          Initializes a CSVWriter with the given Writer.
 
Method Summary
 void close()
          Closes the underlying writer.
protected  String generateDataLine(String... data)
          Converts the String array into a valid CSV form.
 char getEscapeCharacter()
          Returns the current escape character.
 char getQuoteCharacter()
          Returns the current quote character.
 char getSeparator()
          Returns the current separator character.
 boolean isClosed()
          Checks whether the writer was already closed.
protected  String quote(String str)
          Quotes the passed String.
 void setEscapeCharacter(char escapeCharacter)
          Sets the escape character.
 void setQuoteCharacter(char quoteCharacter)
          Sets the quote character.
 void setSeparator(char separator)
          Sets the separator character.
 void write(String... data)
          Converts the passed data and writes the CSV-formatted String into the output.
protected  void write(String str)
          Writes the String into the output.
protected  void writeln(String str)
          Adds a line break to the String and writes it into the output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_SEPARATOR

public static final char DEFAULT_SEPARATOR
The default separator character.

See Also:
Constant Field Values

DEFAULT_QUOTE_CHARACTER

public static final char DEFAULT_QUOTE_CHARACTER
The default quote character.

See Also:
Constant Field Values

DEFAULT_ESCAPE_CHARACTER

public static final char DEFAULT_ESCAPE_CHARACTER
The default escape character.

See Also:
Constant Field Values
Constructor Detail

CSVWriter

public CSVWriter(OutputStream stream)
Initializes a CSVWriter with the given OutputStream.

Parameters:
stream - The stream onto which this instance shall write.

CSVWriter

public CSVWriter(Writer writer)
Initializes a CSVWriter with the given Writer.

Parameters:
writer - The writer that is used for generating the output.
Method Detail

quote

protected String quote(String str)
Quotes the passed String. Besides setting the quote character as a prefix and a suffix of the String; all occurrences within the String will be escaped.

Parameters:
str - The String that shall be passed.
Returns:
The quoted String. If null was passed, an empty String without quotes will be returned.

write

protected void write(String str)
              throws IOException
Writes the String into the output.

Parameters:
str - The String that shall be printed.
Throws:
IOException - If an error occurs during the write process.
IllegalStateException - If the CSVWriter was already closed.

writeln

protected void writeln(String str)
                throws IOException
Adds a line break to the String and writes it into the output.

Parameters:
str - The String that shall be printed.
Throws:
IOException - If an error occurs during the write process.

generateDataLine

protected String generateDataLine(String... data)
Converts the String array into a valid CSV form.

Parameters:
data - The data that shall be converted.
Returns:
The CSV-formatted String.

write

public void write(String... data)
           throws IOException
Converts the passed data and writes the CSV-formatted String into the output.

Parameters:
data - The data that shall be printed.
Throws:
IOException - If an error occurs during the write process.

isClosed

public boolean isClosed()
Checks whether the writer was already closed.

Returns:
true, if the writer was already closed; otherwise false.

close

public void close()
           throws IOException
Closes the underlying writer.

Throws:
IOException - If an error occurs during closing.

getSeparator

public char getSeparator()
Returns the current separator character.

Returns:
The current separator character.

setSeparator

public void setSeparator(char separator)
Sets the separator character.

Parameters:
separator - The separator character to set.

getQuoteCharacter

public char getQuoteCharacter()
Returns the current quote character.

Returns:
The current quote character.

setQuoteCharacter

public void setQuoteCharacter(char quoteCharacter)
Sets the quote character.

Parameters:
quoteCharacter - The quote character to set.

getEscapeCharacter

public char getEscapeCharacter()
Returns the current escape character.

Returns:
The current escape character.

setEscapeCharacter

public void setEscapeCharacter(char escapeCharacter)
Sets the escape character.

Parameters:
escapeCharacter - The escape character to set.


Copyright © 2011 Hasso Plattner Institute - Chair of Information Systems. All Rights Reserved.