Package com.opencsv

Interface ICSVWriter

All Superinterfaces:
AutoCloseable, Closeable, Flushable
All Known Implementing Classes:
AbstractCSVWriter, CSVParserWriter, CSVWriter

public interface ICSVWriter extends Closeable, Flushable
This interface defines all the behavior of a csv writer class.
Since:
4.2
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
    The character used for escaping quotes.
    static final String
    Default line terminator.
    static final char
    The default quote character to use if none is supplied to the constructor.
    static final char
    The default separator to use if none is supplied to the constructor.
    static final int
    Default buffer sizes
    static final char
    The escape constant to use when you wish to suppress all escaping.
    static final char
    The quote constant to use when you wish to suppress all quoting.
    static final String
    RFC 4180 compliant line terminator.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Flushes the buffer and checks to see if the there has been an error in the printstream.
    default void
    Flushes the writer without throwing any exceptions.
    Get latest exception.
    void
    Set the error back to null to be able to check for the next error using checkError().
    void
    Sets the result service.
    default void
    writeAll(Iterable<String[]> allLines)
    Writes iterable to a CSV file.
    void
    writeAll(Iterable<String[]> allLines, boolean applyQuotesToAll)
    Writes iterable to a CSV file.
    default int
    writeAll(ResultSet rs, boolean includeColumnNames)
    Writes the entire ResultSet to a CSV file.
    default int
    writeAll(ResultSet rs, boolean includeColumnNames, boolean trim)
    Writes the entire ResultSet to a CSV file.
    int
    writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll)
    Writes the entire ResultSet to a CSV file.
    default void
    writeAll(List<String[]> allLines)
    Writes the entire list to a CSV file.
    default void
    writeAll(List<String[]> allLines, boolean applyQuotesToAll)
    Writes the entire list to a CSV file.
    default void
    writeNext(String[] nextLine)
    Writes the next line to the file.
    void
    writeNext(String[] nextLine, boolean applyQuotesToAll)
    Writes the next line to the file.

    Methods inherited from interface java.io.Closeable

    close

    Methods inherited from interface java.io.Flushable

    flush
  • Field Details

    • DEFAULT_LINE_END

      static final String DEFAULT_LINE_END
      Default line terminator.
      See Also:
    • RFC4180_LINE_END

      static final String RFC4180_LINE_END
      RFC 4180 compliant line terminator.
      See Also:
    • INITIAL_STRING_SIZE

      static final int INITIAL_STRING_SIZE
      Default buffer sizes
      See Also:
    • DEFAULT_ESCAPE_CHARACTER

      static final char DEFAULT_ESCAPE_CHARACTER
      The character used for escaping quotes.
      See Also:
    • DEFAULT_SEPARATOR

      static final char DEFAULT_SEPARATOR
      The default separator to use if none is supplied to the constructor.
      See Also:
    • DEFAULT_QUOTE_CHARACTER

      static final char DEFAULT_QUOTE_CHARACTER
      The default quote character to use if none is supplied to the constructor.
      See Also:
    • NO_QUOTE_CHARACTER

      static final char NO_QUOTE_CHARACTER
      The quote constant to use when you wish to suppress all quoting.
      See Also:
    • NO_ESCAPE_CHARACTER

      static final char NO_ESCAPE_CHARACTER
      The escape constant to use when you wish to suppress all escaping.
      See Also:
  • Method Details

    • writeAll

      void writeAll(Iterable<String[]> allLines, boolean applyQuotesToAll)
      Writes iterable to a CSV file. The list is assumed to be a String[]
      Parameters:
      allLines - an Iterable of String[], with each String[] representing a line of the file.
      applyQuotesToAll - true if all values are to be quoted. false if quotes only to be applied to values which contain the separator, escape, quote or new line characters.
    • writeAll

      default void writeAll(List<String[]> allLines, boolean applyQuotesToAll)
      Writes the entire list to a CSV file. The list is assumed to be a String[].
      Parameters:
      allLines - A List of String[] with each String[] representing a line of the file.
      applyQuotesToAll - True if all values are to be quoted. False if quotes only to be applied to values which contain the separator, escape, quote, or new line characters.
    • writeAll

      default void writeAll(Iterable<String[]> allLines)
      Writes iterable to a CSV file. The list is assumed to be a String[]
      Parameters:
      allLines - an Iterable of String[], with each String[] representing a line of the file.
    • writeAll

      default void writeAll(List<String[]> allLines)
      Writes the entire list to a CSV file. The list is assumed to be a String[].
      Parameters:
      allLines - A List of String[] with each String[] representing a line of the file.
    • writeAll

      default int writeAll(ResultSet rs, boolean includeColumnNames) throws SQLException, IOException
      Writes the entire ResultSet to a CSV file.

      The caller is responsible for closing the ResultSet. Values are not trimmed. Quotes are applied to all values in the output.

      Parameters:
      rs - The result set to write
      includeColumnNames - True if you want column names in the output, false otherwise
      Returns:
      Number of lines written.
      Throws:
      IOException - Thrown by ResultSetHelper.getColumnValues()
      SQLException - Thrown by ResultSetHelper.getColumnValues()
    • writeAll

      default int writeAll(ResultSet rs, boolean includeColumnNames, boolean trim) throws SQLException, IOException
      Writes the entire ResultSet to a CSV file.

      The caller is responsible for closing the ResultSet. Quotes are applied to all values in the output.

      Parameters:
      rs - The Result set to write.
      includeColumnNames - Include the column names in the output.
      trim - Remove spaces from the data before writing.
      Returns:
      Number of lines written - including header.
      Throws:
      IOException - Thrown by ResultSetHelper.getColumnValues()
      SQLException - Thrown by ResultSetHelper.getColumnValues()
    • writeAll

      int writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws SQLException, IOException
      Writes the entire ResultSet to a CSV file. The caller is responsible for closing the ResultSet.
      Parameters:
      rs - The Result set to write.
      includeColumnNames - Include the column names in the output.
      trim - Remove spaces from the data before writing.
      applyQuotesToAll - Whether all values should be quoted.
      Returns:
      Number of lines written - including header.
      Throws:
      IOException - Thrown by ResultSetHelper.getColumnValues()
      SQLException - Thrown by ResultSetHelper.getColumnValues()
    • writeNext

      void writeNext(String[] nextLine, boolean applyQuotesToAll)
      Writes the next line to the file.
      Parameters:
      nextLine - A string array with each comma-separated element as a separate entry.
      applyQuotesToAll - True if all values are to be quoted. False applies quotes only to values which contain the separator, escape, quote, or new line characters.
    • writeNext

      default void writeNext(String[] nextLine)
      Writes the next line to the file.
      Parameters:
      nextLine - A string array with each comma-separated element as a separate entry.
    • checkError

      boolean checkError()
      Flushes the buffer and checks to see if the there has been an error in the printstream.
      Returns:
      True if the print stream has encountered an error either on the underlying output stream or during a format conversion.
    • getException

      IOException getException()
      Get latest exception.

      NOTE: This does not return exception which are caught by underlying writer (PrintWriter) or stream. If you are using this method then consider using a Writer class that throws exceptions.

      Returns:
      the latest IOException encountered in the print stream either on the underlying output stream or during a format conversion.
    • resetError

      void resetError()
      Set the error back to null to be able to check for the next error using checkError().
    • setResultService

      void setResultService(ResultSetHelper resultService)
      Sets the result service.
      Parameters:
      resultService - The ResultSetHelper
    • flushQuietly

      default void flushQuietly()
      Flushes the writer without throwing any exceptions.