CSVPrinter (Apache Commons CSV 1.0 API)

org.apache.commons.csv

## Class CSVPrinter

- java.lang.Object
- - org.apache.commons.csv.CSVPrinter

- All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

      public final class CSVPrinter
      extends Object
      implements Flushable, Closeable

  Prints values in a CSV format.
  Version:
    $Id: CSVPrinter.java 1617076 2014-08-10 09:23:01Z britter $

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `CSVPrinter(Appendable out, CSVFormat format)` Creates a printer that will print values to the given stream following the CSVFormat.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `close()`
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### CSVPrinter

          public CSVPrinter(Appendable out,
                            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.

      Parameters:
        `out` - stream to which to print. Must not be null.
        `format` - the CSV format. Must not be null.
      Throws:
        `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.

  - ### Method Detail

    - #### close

          public void close()
                     throws IOException

      Specified by:
        `close` in interface `Closeable`
      Specified by:
        `close` in interface `AutoCloseable`
      Throws:
        `IOException`

    - #### flush

          public void flush()
                     throws IOException

      Flushes the underlying stream.
      Specified by:
        `flush` in interface `Flushable`
      Throws:
        `IOException` - If an I/O error occurs

    - #### print

          public void print(Object value)
                     throws IOException

      Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
      Parameters:
        `value` - value to be output.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printComment

          public void printComment(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 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.
      Parameters:
        `comment` - the comment to output
      Throws:
        `IOException` - If an I/O error occurs

    - #### println

          public void println()
                       throws IOException

      Outputs the record separator.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printRecord

          public void printRecord(Iterable<?> values)
                           throws IOException

      Prints the given values 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 `println()`.

      Parameters:
        `values` - values to output.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printRecord

          public void printRecord(Object... values)
                           throws IOException

      Prints the given values 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 `println()`.

      Parameters:
        `values` - values to output.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printRecords

          public void printRecords(Iterable<?> values)
                            throws IOException

      Prints all the objects in the given collection handling nested collections/arrays as records.

      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

      Parameters:
        `values` - the values to print.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printRecords

          public void printRecords(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 `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

      Parameters:
        `values` - the values to print.
      Throws:
        `IOException` - If an I/O error occurs

    - #### printRecords

          public void printRecords(ResultSet resultSet)
                            throws SQLException,
                                   IOException

      Prints all the objects in the given JDBC result set.
      Parameters:
        `resultSet` - result set the values to print.
      Throws:
        `IOException` - If an I/O error occurs
        `SQLException` - if a database access error occurs

    - #### getOut

          public Appendable getOut()

      Gets the target Appendable.
      Returns:
        the target Appendable.

Copyright © 2014 The Apache Software Foundation. All rights reserved.