public final class CSVPrinter extends Object implements Flushable, Closeable
CSV format
.
Values can be appended to the output by calling the print(Object)
method.
Values are printed according to String.valueOf(Object)
.
To complete a record the println()
method has to be called.
Comments can be appended by calling printComment(String)
.
However a comment will only be written to the output if the CSVFormat
supports comments.
The printer also supports appending a complete record at once by calling printRecord(Object...)
or printRecord(Iterable)
.
Furthermore printRecords(Object...)
, printRecords(Iterable)
and printRecords(ResultSet)
methods can be used to print several records at once.
Example:
try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.EXCEL)) { printer.printRecord("id", "userName", "firstName", "lastName", "birthday"); printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15)); printer.println(); printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29)); } catch (IOException ex) { ex.printStackTrace(); }
This code will write the following to csv.txt:
id,userName,firstName,lastName,birthday 1,john73,John,Doe,1973-09-15 2,mary,Mary,Meyer,1985-03-29
Constructor and Description |
---|
CSVPrinter(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 |
close(boolean flush)
Closes the underlying stream with an optional flush first.
|
void |
flush()
Flushes the underlying stream.
|
Appendable |
getOut()
Gets the target Appendable.
|
void |
print(Object value)
Prints the string as the next value on the line.
|
void |
printComment(String comment)
Prints a comment on a new line among the delimiter separated values.
|
void |
println()
Outputs the record separator.
|
void |
printRecord(Iterable<?> values)
Prints the given values a single record of delimiter separated values followed by the record separator.
|
void |
printRecord(Object... values)
Prints the given values a single record of delimiter separated values followed by the record separator.
|
void |
printRecords(Iterable<?> values)
Prints all the objects in the given collection handling nested collections/arrays as records.
|
void |
printRecords(Object... values)
Prints all the objects in the given array handling nested collections/arrays as records.
|
void |
printRecords(ResultSet resultSet)
Prints all the objects in the given JDBC result set.
|
public CSVPrinter(Appendable out, CSVFormat format) throws 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.IOException
- thrown if the optional header cannot be printed.IllegalArgumentException
- thrown if the parameters of the format are inconsistent or if either out or format are null.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
public void close(boolean flush) throws IOException
flush
- whether to flush before the actual close.IOException
- If an I/O error occurspublic void flush() throws IOException
flush
in interface Flushable
IOException
- If an I/O error occurspublic Appendable getOut()
public void print(Object value) throws IOException
value
- value to be output.IOException
- If an I/O error occurspublic void printComment(String comment) throws IOException
Comments will always begin on a new line and occupy at 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.
This method detects line breaks inside the comment string and inserts CSVFormat.getRecordSeparator()
to start a new line of the comment. Note that this might produce unexpected results for formats that do not use
line breaks as record separator.
comment
- the comment to outputIOException
- If an I/O error occurspublic void println() throws IOException
IOException
- If an I/O error occurspublic void printRecord(Iterable<?> values) throws 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.IOException
- If an I/O error occurspublic void printRecord(Object... values) throws 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.IOException
- If an I/O error occurspublic void printRecords(Iterable<?> values) throws 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.IOException
- If an I/O error occurspublic void printRecords(Object... values) throws 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.IOException
- If an I/O error occurspublic void printRecords(ResultSet resultSet) throws SQLException, IOException
resultSet
- result set the values to print.IOException
- If an I/O error occursSQLException
- if a database access error occursCopyright © 2019 The Apache Software Foundation. All rights reserved.