com.Ostermiller.util
Class CSVPrinter

java.lang.Object
  extended by com.Ostermiller.util.CSVPrinter
All Implemented Interfaces:
CSVPrint

public class CSVPrinter
extends java.lang.Object
implements CSVPrint

Print values as a comma separated list. More information about this class is available from ostermiller.org.

Since:
ostermillerutils 1.00.00

Field Summary
protected  boolean alwaysQuote
          If auto flushing is enabled.
protected  boolean autoFlush
          If auto flushing is enabled.
protected  char commentStart
          Character used to start comments.
protected  char delimiterChar
          Character written as field delimiter.
protected  boolean error
          true iff an error has occurred.
protected  java.lang.String lineEnding
          The line ending, must be one of "\n", "\r", or "\r\n"
protected  boolean newLine
          True iff we just began a new line.
protected  java.io.Writer out
          The place that the values get written.
protected  char quoteChar
          Quoting character.
 
Constructor Summary
CSVPrinter(java.io.OutputStream out)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.OutputStream out, char commentStart)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, boolean alwaysQuote, boolean autoFlush)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, char commentStart)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, char commentStart, char quote, char delimiter)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, char commentStart, char quote, char delimiter, boolean alwaysQuote, boolean autoFlush)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, char commentStart, char quote, char delimiter, java.lang.String lineEnding)
          Create a printer that will print values to the given stream.
CSVPrinter(java.io.Writer out, char commentStart, char quote, char delimiter, java.lang.String lineEnding, boolean alwaysQuote, boolean autoFlush)
          Create a printer that will print values to the given stream.
 
Method Summary
 void changeDelimiter(char newDelimiter)
          Change this printer so that it uses a new delimiter.
 void changeQuote(char newQuote)
          Change this printer so that it uses a new character for quoting.
 boolean checkError()
          Flush the stream if it's not closed and check its error state.
 void close()
          Close any underlying streams.
 void flush()
          Flush any data written out to underlying streams.
 void print(java.lang.String value)
          Print the string as the next value on the line.
 void print(java.lang.String[] values)
          Print a single line of comma separated values.
 void println()
          Output a blank line.
 void println(java.lang.String value)
          Print the string as the last value on the line.
 void println(java.lang.String[] values)
          Print a single line of comma separated values.
 void println(java.lang.String[][] values)
          Print several lines of comma separated values.
 void printlnComment(java.lang.String comment)
          Put a comment among the comma separated values.
 void setAlwaysQuote(boolean alwaysQuote)
          Set whether values printers should always be quoted, or whether the printer may, at its discretion, omit quotes around the value.
 void setAutoFlush(boolean autoFlush)
          Set flushing behavior.
 void setLineEnding(java.lang.String lineEnding)
          Change this printer so that it uses a new line ending.
 void write(java.lang.String value)
          Print the string as the next value on the line.
 void write(java.lang.String[] values)
          Print a single line of comma separated values.
 void writeln()
          Output a blank line.
 void writeln(java.lang.String value)
          Print the string as the last value on the line.
 void writeln(java.lang.String[] values)
          Print a single line of comma separated values.
 void writeln(java.lang.String[][] values)
          Print several lines of comma separated values.
 void writelnComment(java.lang.String comment)
          Put a comment among the comma separated values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

autoFlush

protected boolean autoFlush
If auto flushing is enabled.

Since:
ostermillerutils 1.02.26

alwaysQuote

protected boolean alwaysQuote
If auto flushing is enabled.

Since:
ostermillerutils 1.02.26

error

protected boolean error
true iff an error has occurred.

Since:
ostermillerutils 1.02.26

delimiterChar

protected char delimiterChar
Character written as field delimiter.

Since:
ostermillerutils 1.02.18

quoteChar

protected char quoteChar
Quoting character.

Since:
ostermillerutils 1.02.18

out

protected java.io.Writer out
The place that the values get written.

Since:
ostermillerutils 1.00.00

newLine

protected boolean newLine
True iff we just began a new line.

Since:
ostermillerutils 1.00.00

commentStart

protected char commentStart
Character used to start comments. (Default is '#')

Since:
ostermillerutils 1.00.00

lineEnding

protected java.lang.String lineEnding
The line ending, must be one of "\n", "\r", or "\r\n"

Since:
ostermillerutils 1.06.01
Constructor Detail

CSVPrinter

public CSVPrinter(java.io.OutputStream out)
Create a printer that will print values to the given stream. Character to byte conversion is done using the default character encoding. Comments will be written using the default comment character '#', the delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
Since:
ostermillerutils 1.00.00

CSVPrinter

public CSVPrinter(java.io.Writer out)
Create a printer that will print values to the given stream. Comments will be written using the default comment character '#', the delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
Since:
ostermillerutils 1.00.00

CSVPrinter

public CSVPrinter(java.io.OutputStream out,
                  char commentStart)
Create a printer that will print values to the given stream. Character to byte conversion is done using the default character encoding. The delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
Since:
ostermillerutils 1.00.00

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  char commentStart)
Create a printer that will print values to the given stream. The delimiter will be the comma, the line ending will be the default system line ending, the quote character will be double quotes, quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
Since:
ostermillerutils 1.00.00

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  boolean alwaysQuote,
                  boolean autoFlush)
Create a printer that will print values to the given stream. The comment character will be the number sign, the delimiter will be the comma, the line ending will be the default system line ending, and the quote character will be double quotes.

Parameters:
out - stream to which to print.
alwaysQuote - true if quotes should be used even when not strictly needed.
autoFlush - should auto flushing be enabled.
Since:
ostermillerutils 1.02.26

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  char commentStart,
                  char quote,
                  char delimiter)
           throws BadDelimiterException,
                  BadQuoteException
Create a printer that will print values to the given stream. The line ending will be the default system line ending, quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
delimiter - The new delimiter character to use.
quote - The new character to use for quoting.
Throws:
BadQuoteException - if the character cannot be used as a quote.
BadDelimiterException - if the character cannot be used as a delimiter.
Since:
ostermillerutils 1.02.26

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  char commentStart,
                  char quote,
                  char delimiter,
                  java.lang.String lineEnding)
           throws BadDelimiterException,
                  BadQuoteException,
                  BadLineEndingException
Create a printer that will print values to the given stream. Quotes will be used when needed, and auto flushing will be enabled.

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
delimiter - The new delimiter character to use.
quote - The new character to use for quoting.
lineEnding - The new line ending, or null to use the default line ending.
Throws:
BadQuoteException - if the character cannot be used as a quote.
BadDelimiterException - if the character cannot be used as a delimiter.
BadLineEndingException - if the line ending is not one of the three legal line endings.
Since:
ostermillerutils 1.06.01

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  char commentStart,
                  char quote,
                  char delimiter,
                  boolean alwaysQuote,
                  boolean autoFlush)
           throws BadDelimiterException,
                  BadQuoteException
Create a printer that will print values to the given stream. The line ending will be the default system line ending,

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
delimiter - The new delimiter character to use.
quote - The new character to use for quoting.
alwaysQuote - true if quotes should be used even when not strictly needed.
autoFlush - should auto flushing be enabled.
Throws:
BadQuoteException - if the character cannot be used as a quote.
BadDelimiterException - if the character cannot be used as a delimiter.
Since:
ostermillerutils 1.02.26

CSVPrinter

public CSVPrinter(java.io.Writer out,
                  char commentStart,
                  char quote,
                  char delimiter,
                  java.lang.String lineEnding,
                  boolean alwaysQuote,
                  boolean autoFlush)
           throws BadDelimiterException,
                  BadQuoteException,
                  BadLineEndingException
Create a printer that will print values to the given stream.

Parameters:
out - stream to which to print.
commentStart - Character used to start comments.
delimiter - The new delimiter character to use.
lineEnding - The new line ending, or null to use the default line ending.
quote - The new character to use for quoting.
alwaysQuote - true if quotes should be used even when not strictly needed.
autoFlush - should auto flushing be enabled.
Throws:
BadQuoteException - if the character cannot be used as a quote.
BadDelimiterException - if the character cannot be used as a delimiter.
BadLineEndingException - if the line ending is not one of the three legal line endings.
Since:
ostermillerutils 1.06.01
Method Detail

changeDelimiter

public void changeDelimiter(char newDelimiter)
                     throws BadDelimiterException
Change this printer so that it uses a new delimiter.

Specified by:
changeDelimiter in interface CSVPrint
Parameters:
newDelimiter - The new delimiter character to use.
Throws:
BadDelimiterException - if the character cannot be used as a delimiter.
Since:
ostermillerutils 1.02.18

changeQuote

public void changeQuote(char newQuote)
                 throws BadQuoteException
Change this printer so that it uses a new character for quoting.

Specified by:
changeQuote in interface CSVPrint
Parameters:
newQuote - The new character to use for quoting.
Throws:
BadQuoteException - if the character cannot be used as a quote.
Since:
ostermillerutils 1.02.18

setLineEnding

public void setLineEnding(java.lang.String lineEnding)
                   throws BadLineEndingException
Change this printer so that it uses a new line ending.

A line ending must be one of "\n", "\r", or "\r\n".

The default line ending is the system line separator as specified by System.getProperty("line.separator"), or "\n" if the system line separator is not a legal line ending.

Parameters:
lineEnding - The new line ending, or null to use the default line ending.
Throws:
BadLineEndingException - if the line ending is not one of the three legal line endings.
Since:
ostermillerutils 1.06.01

println

public void println(java.lang.String value)
Print the string as the last value on the line. The value will be quoted if needed.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

Specified by:
println in interface CSVPrint
Parameters:
value - value to be outputted.
Since:
ostermillerutils 1.00.00

writeln

public void writeln(java.lang.String value)
             throws java.io.IOException
Print the string as the last value on the line. The value will be quoted if needed.

Specified by:
writeln in interface CSVPrint
Parameters:
value - value to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

println

public void println()
Output a blank line.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

Specified by:
println in interface CSVPrint
Since:
ostermillerutils 1.00.00

writeln

public void writeln()
             throws java.io.IOException
Output a blank line.

Specified by:
writeln in interface CSVPrint
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

println

public void println(java.lang.String[] values)
Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

Specified by:
println in interface CSVPrint
Parameters:
values - values to be outputted.
Since:
ostermillerutils 1.00.00

writeln

public void writeln(java.lang.String[] values)
             throws java.io.IOException
Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

Specified by:
writeln in interface CSVPrint
Parameters:
values - values to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

print

public void print(java.lang.String[] values)
Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

Specified by:
print in interface CSVPrint
Parameters:
values - values to be outputted.
Since:
ostermillerutils 1.00.00

write

public void write(java.lang.String[] values)
           throws java.io.IOException
Print a single line of comma separated values. The values will be quoted if needed. Quotes and and other characters that need it will be escaped.

Specified by:
write in interface CSVPrint
Parameters:
values - values to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

println

public void println(java.lang.String[][] values)
Print several lines of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writeln method.

Specified by:
println in interface CSVPrint
Parameters:
values - values to be outputted.
Since:
ostermillerutils 1.00.00

writeln

public void writeln(java.lang.String[][] values)
             throws java.io.IOException
Print several lines of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.

Specified by:
writeln in interface CSVPrint
Parameters:
values - values to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

printlnComment

public void printlnComment(java.lang.String comment)
Put a comment among the comma separated values. Comments will always begin on a new line and occupy a least one full line. The character specified to star comments and a space will be inserted at the beginning of each new line in the comment. If the comment is null, an empty comment is outputted.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding writelnComment method.

Specified by:
printlnComment in interface CSVPrint
Parameters:
comment - the comment to output.
Since:
ostermillerutils 1.00.00

writelnComment

public void writelnComment(java.lang.String comment)
                    throws java.io.IOException
Put a comment among the comma separated values. Comments will always begin on a new line and occupy a least one full line. The character specified to star comments and a space will be inserted at the beginning of each new line in the comment. If the comment is null, an empty comment is outputted.

Specified by:
writelnComment in interface CSVPrint
Parameters:
comment - the comment to output.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

print

public void print(java.lang.String value)
Print the string as the next value on the line. The value will be quoted if needed. If value is null, an empty value is printed.

This method never throws an I/O exception. The client may inquire as to whether any errors have occurred by invoking checkError(). If an I/O Exception is desired, the client should use the corresponding println method.

Specified by:
print in interface CSVPrint
Parameters:
value - value to be outputted.
Since:
ostermillerutils 1.00.00

write

public void write(java.lang.String value)
           throws java.io.IOException
Print the string as the next value on the line. The value will be quoted if needed. If value is null, an empty value is printed.

Specified by:
write in interface CSVPrint
Parameters:
value - value to be outputted.
Throws:
java.io.IOException - if an error occurs while writing.
Since:
ostermillerutils 1.02.26

flush

public void flush()
           throws java.io.IOException
Flush any data written out to underlying streams.

Specified by:
flush in interface CSVPrint
Throws:
java.io.IOException - if IO error occurs
Since:
ostermillerutils 1.02.26

close

public void close()
           throws java.io.IOException
Close any underlying streams.

Specified by:
close in interface CSVPrint
Throws:
java.io.IOException - if IO error occurs
Since:
ostermillerutils 1.02.26

checkError

public boolean checkError()
Flush the stream if it's not closed and check its error state. Errors are cumulative; once the stream encounters an error, this routine will return true on all successive calls.

Specified by:
checkError in interface CSVPrint
Returns:
True if the print stream has encountered an error, either on the underlying output stream or during a format conversion.
Since:
ostermillerutils 1.02.26

setAutoFlush

public void setAutoFlush(boolean autoFlush)
Set flushing behavior. Iff set, a flush command will be issued to any underlying stream after each print or write command.

Specified by:
setAutoFlush in interface CSVPrint
Parameters:
autoFlush - should auto flushing be enabled.
Since:
ostermillerutils 1.02.26

setAlwaysQuote

public void setAlwaysQuote(boolean alwaysQuote)
Set whether values printers should always be quoted, or whether the printer may, at its discretion, omit quotes around the value.

Specified by:
setAlwaysQuote in interface CSVPrint
Parameters:
alwaysQuote - true if quotes should be used even when not strictly needed.
Since:
ostermillerutils 1.02.26