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
| Modifier and Type | Field and Description |
|---|---|
| private final Appendable | appendable
The place that the values get written. |
| private final CSVFormat | |
| private boolean | newRecord
True if we just began a new record. |
| private long |
| Access | Constructor and Description |
|---|---|
| public | CSVPrinter(final Appendable
stream to which to print. Must not be null. appendable, final CSVFormat the CSV format. Must not be null. format)Creates a printer that will print values to the given stream following the CSVFormat. |
| Modifier and Type | Method and Description |
|---|---|
| public void | close()
Implements java. Closes this stream and releases any system resources associated with it. |
| public void | close(final boolean
whether to flush before the actual close. flush)Closes the underlying stream with an optional flush first. |
| private synchronized void | |
| public void | |
| public Appendable | |
| public long | Returns: the record count, this does not include comments or headers.Gets the record count printed, this does not include comments or headers. |
| public synchronized void | |
| public synchronized void | printComment(final String
the comment to output comment)Prints a comment on a new line among the delimiter-separated values. |
| public synchronized void | printHeaders(final ResultSet
The ResultSet to query for metadata. resultSet)Prints headers for a result set based on its metadata. |
| public synchronized void | |
| public synchronized void | printRecord(final Iterable<?>
values to output. values)Prints the given values as a single record of delimiter-separated values followed by the record separator. |
| public void | printRecord(final Object...
values to output. values)Prints the given values as a single record of delimiter-separated values followed by the record separator. |
| public synchronized void | printRecord(final Stream<?>
values to output. values)Prints the given values as a single record of delimiter-separated values followed by the record separator. |
| private void | |
| private void | |
| public void | printRecords(final Iterable<?>
the values to print. values)Prints all the objects in the given |
| public void | printRecords(final Object...
the values to print. values)Prints all the objects in the given array handling nested collections/arrays as records. |
| public void | printRecords(final ResultSet
The values to print. resultSet)Prints all the objects in the given JDBC result set. |
| public void | printRecords(final ResultSet
source of row data. resultSet, final boolean whether to print headers. printHeader)Prints all the objects with metadata in the given JDBC result set based on the header boolean. |
| public void | printRecords(final Stream<?>
the values to print. values)Prints all the objects in the given |
| appendable | back to summary |
|---|---|
| private final Appendable appendable The place that the values get written. | |
| format | back to summary |
|---|---|
| private final CSVFormat format | |
| newRecord | back to summary |
|---|---|
| private boolean newRecord True if we just began a new record. | |
| recordCount | back to summary |
|---|---|
| private long recordCount | |
| CSVPrinter | back to summary |
|---|---|
| public CSVPrinter(final Appendable appendable, final CSVFormat format) throws IOException Creates a printer that will print values to the given stream following the CSVFormat. 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.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Doc from java. Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect. As noted in
| |
| close | back to summary |
|---|---|
| public void close(final boolean flush) throws IOException Closes the underlying stream with an optional flush first.
| |
| endOfRecord | back to summary |
|---|---|
| private synchronized void endOfRecord() throws IOException Prints the record separator and increments the record count.
| |
| flush | back to summary |
|---|---|
| public void flush() throws IOException Implements java. Flushes the underlying stream.
| |
| getOut | back to summary |
|---|---|
| public Appendable getOut() Gets the target Appendable.
| |
| getRecordCount | back to summary |
|---|---|
| public long getRecordCount() Gets the record count printed, this does not include comments or headers.
| |
| back to summary | |
|---|---|
| public synchronized void print(final Object value) throws IOException Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
| |
| printComment | back to summary |
|---|---|
| public synchronized void printComment(final String comment) throws IOException Prints a comment on a new line among the delimiter-separated values. 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
| |
| printHeaders | back to summary |
|---|---|
| public synchronized void printHeaders(final ResultSet resultSet) throws IOException, SQLException Prints headers for a result set based on its metadata.
| |
| println | back to summary |
|---|---|
| public synchronized void println() throws IOException Prints the record separator.
| |
| printRecord | back to summary |
|---|---|
| public synchronized void printRecord(final Iterable<?> values) throws IOException Prints the given values as a single record of delimiter-separated values followed by the record separator.
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
| |
| printRecord | back to summary |
|---|---|
| public void printRecord(final Object... values) throws IOException Prints the given values as a single record of delimiter-separated values followed by the record separator.
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
| |
| printRecord | back to summary |
|---|---|
| public synchronized void printRecord(final Stream<?> values) throws IOException Prints the given values as a single record of delimiter-separated values followed by the record separator.
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
| |
| printRecordObject | back to summary |
|---|---|
| private void printRecordObject(final Object value) throws IOException | |
| printRecords | back to summary |
|---|---|
private void printRecords(final IOStream<?> stream) throws IOException
| |
| printRecords | back to summary |
|---|---|
| public void printRecords(final Iterable<?> values) throws IOException Prints all the objects in the given
If the given Iterable only contains simple objects, this method will print a single record like
Given the following data structure:
Calling this method will print:
| |
| printRecords | back to summary |
|---|---|
| public void printRecords(final Object... values) throws IOException Prints all the objects in the given array handling nested collections/arrays as records.
If the given array only contains simple objects, this method will print a single record like
Given the following data structure:
Calling this method will print:
| |
| printRecords | back to summary |
|---|---|
| public void printRecords(final ResultSet resultSet) throws SQLException, IOException Prints all the objects in the given JDBC result set.
You can use
| |
| printRecords | back to summary |
|---|---|
| public void printRecords(final ResultSet resultSet, final boolean printHeader) throws SQLException, IOException Prints all the objects with metadata in the given JDBC result set based on the header boolean.
You can use
| |
| printRecords | back to summary |
|---|---|
| public void printRecords(final Stream<?> values) throws IOException Prints all the objects in the given
If the given Stream only contains simple objects, this method will print a single record like
Given the following data structure:
Calling this method will print:
| |