public final class CSVPrinter
extends java.lang.Object
implements java.io.Flushable, java.io.Closeable
| Modifier and Type | Field and Description |
|---|---|
private CSVFormat |
format |
private boolean |
newRecord
True if we just began a new record.
|
private java.lang.Appendable |
out
The place that the values get written.
|
| Constructor and Description |
|---|
CSVPrinter(java.lang.Appendable out,
CSVFormat format)
Creates a printer that will print values to the given stream following the CSVFormat.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
flush()
Flushes the underlying stream.
|
java.lang.Appendable |
getOut()
Gets the target Appendable.
|
void |
print(java.lang.Object value)
Prints the string as the next value on the line.
|
void |
printComment(java.lang.String comment)
Prints a comment on a new line among the delimiter separated values.
|
void |
println()
Outputs the record separator.
|
void |
printRecord(java.lang.Iterable<?> values)
Prints the given values a single record of delimiter separated values followed by the record separator.
|
void |
printRecord(java.lang.Object... values)
Prints the given values a single record of delimiter separated values followed by the record separator.
|
void |
printRecords(java.lang.Iterable<?> values)
Prints all the objects in the given collection handling nested collections/arrays as records.
|
void |
printRecords(java.lang.Object... values)
Prints all the objects in the given array handling nested collections/arrays as records.
|
void |
printRecords(java.sql.ResultSet resultSet)
Prints all the objects in the given JDBC result set.
|
private final java.lang.Appendable out
private final CSVFormat format
private boolean newRecord
public CSVPrinter(java.lang.Appendable out,
CSVFormat format)
throws java.io.IOException
Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported.
out - stream to which to print. Must not be null.format - the CSV format. Must not be null.java.io.IOException - thrown if the optional header cannot be printed.java.lang.IllegalArgumentException - thrown if the parameters of the format are inconsistent or if either out or format are null.public void close()
throws java.io.IOException
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablejava.io.IOExceptionpublic void flush()
throws java.io.IOException
flush in interface java.io.Flushablejava.io.IOException - If an I/O error occurspublic java.lang.Appendable getOut()
public void print(java.lang.Object value)
throws java.io.IOException
value - value to be output.java.io.IOException - If an I/O error occurspublic void printComment(java.lang.String comment)
throws java.io.IOException
Comments will always begin on a new line and occupy a least one full line. The character specified to start comments and a space will be inserted at the beginning of each new line in the comment.
If comments are disabled in the current CSV format this method does nothing.comment - the comment to outputjava.io.IOException - If an I/O error occurspublic void println()
throws java.io.IOException
java.io.IOException - If an I/O error occurspublic void printRecord(java.lang.Iterable<?> values)
throws java.io.IOException
The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
separator to the output after printing the record, so there is no need to call println().
values - values to output.java.io.IOException - If an I/O error occurspublic void printRecord(java.lang.Object... values)
throws java.io.IOException
The values will be quoted if needed. Quotes and newLine characters will be escaped. This method adds the record
separator to the output after printing the record, so there is no need to call println().
values - values to output.java.io.IOException - If an I/O error occurspublic void printRecords(java.lang.Iterable<?> values)
throws java.io.IOException
If the given collection only contains simple objects, this method will print a single record like
printRecord(Iterable). If the given collections contains nested collections/arrays those nested elements
will each be printed as records using printRecord(Object...).
Given the following data structure:
List<String[]> data = ...
data.add(new String[]{ "A", "B", "C" });
data.add(new String[]{ "1", "2", "3" });
data.add(new String[]{ "A1", "B2", "C3" });
Calling this method will print:
A, B, C
1, 2, 3
A1, B2, C3
values - the values to print.java.io.IOException - If an I/O error occurspublic void printRecords(java.lang.Object... values)
throws java.io.IOException
If the given array only contains simple objects, this method will print a single record like
printRecord(Object...). If the given collections contains nested collections/arrays those nested
elements will each be printed as records using printRecord(Object...).
Given the following data structure:
String[][] data = new String[3][]
data[0] = String[]{ "A", "B", "C" };
data[1] = new String[]{ "1", "2", "3" };
data[2] = new String[]{ "A1", "B2", "C3" };
Calling this method will print:
A, B, C
1, 2, 3
A1, B2, C3
values - the values to print.java.io.IOException - If an I/O error occurspublic void printRecords(java.sql.ResultSet resultSet)
throws java.sql.SQLException,
java.io.IOException
resultSet - result set the values to print.java.io.IOException - If an I/O error occursjava.sql.SQLException - if a database access error occurs