org.apache.commons.csv

Class CSVPrinter


public class CSVPrinter
extends java.lang.Object

Print values as a comma separated list.

Field Summary

protected boolean
newLine
True if we just began a new line.
protected PrintWriter
out
The place that the values get written.
private CSVStrategy
strategy

Constructor Summary

CSVPrinter(OutputStream out)
Create a printer that will print values to the given stream.
CSVPrinter(Writer out)
Create a printer that will print values to the given stream.

Method Summary

private String
escapeAndQuote(String value)
Enclose the value in quotes and escape the quote and comma characters that are inside.
CSVStrategy
getStrategy()
Obtain the specified CSV Strategy
void
print(String value)
Print the string as the next value on the line.
void
println()
Output a blank line
void
println(String value)
Print the string as the last value on the line.
void
println(String[] values)
Print a single line of comma separated values.
void
println(String[][] values)
Print several lines of comma separated values.
void
printlnComment(String comment)
Put a comment among the comma separated values.
CSVPrinter
setStrategy(CSVStrategy strategy)
Sets the specified CSV Strategy

Field Details

newLine

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

out

protected PrintWriter out
The place that the values get written.

strategy

private CSVStrategy strategy

Constructor Details

CSVPrinter

public CSVPrinter(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 '#'.
Parameters:
out - stream to which to print.

CSVPrinter

public CSVPrinter(Writer out)
Create a printer that will print values to the given stream. Comments will be written using the default comment character '#'.
Parameters:
out - stream to which to print.

Method Details

escapeAndQuote

private String escapeAndQuote(String value)
Enclose the value in quotes and escape the quote and comma characters that are inside.
Parameters:
value - needs to be escaped and quoted
Returns:
the value, escaped and quoted

getStrategy

public CSVStrategy getStrategy()
Obtain the specified CSV Strategy
Returns:
strategy currently being used

print

public void print(String value)
Print the string as the next value on the line. The value will be quoted if needed.
Parameters:
value - value to be outputted.

println

public void println()
Output a blank line

println

public void println(String value)
Print the string as the last value on the line. The value will be quoted if needed.
Parameters:
value - value to be outputted.

println

public void println(String[] values)
Print a single line of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.
Parameters:
values - values to be outputted.

println

public void println(String[][] values)
Print several lines of comma separated values. The values will be quoted if needed. Quotes and newLine characters will be escaped.
Parameters:
values - values to be outputted.

printlnComment

public void printlnComment(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.
Parameters:
comment - the comment to output

setStrategy

public CSVPrinter setStrategy(CSVStrategy strategy)
Sets the specified CSV Strategy
Returns:
current instance of CSVParser to allow chained method calls