Top Description Fields Methods
com.opencsv

public Interface ICSVWriter

extends Closeable, Flushable
Known Direct Implementers
com.opencsv.AbstractCSVWriter
Imports
java.io.Closeable, .Flushable, .IOException, java.sql.ResultSet, .SQLException, java.util.List

This interface defines all the behavior of a csv writer class.
Since
4.2

Field Summary

Modifier and TypeField and Description
public static final char
DEFAULT_ESCAPE_CHARACTER

The character used for escaping quotes.

public static final String
DEFAULT_LINE_END

Default line terminator.

public static final char
DEFAULT_QUOTE_CHARACTER

The default quote character to use if none is supplied to the constructor.

public static final char
DEFAULT_SEPARATOR

The default separator to use if none is supplied to the constructor.

public static final int
INITIAL_STRING_SIZE

Default buffer sizes

public static final char
NO_ESCAPE_CHARACTER

The escape constant to use when you wish to suppress all escaping.

public static final char
NO_QUOTE_CHARACTER

The quote constant to use when you wish to suppress all quoting.

public static final String
RFC4180_LINE_END

RFC 4180 compliant line terminator.

Method Summary

Modifier and TypeMethod and Description
public boolean

Returns:

True if the print stream has encountered an error either on the underlying output stream or during a format conversion.
checkError
()

Flushes the buffer and checks to see if the there has been an error in the printstream.

public default void
flushQuietly()

Flushes the writer without throwing any exceptions.

public IOException

Returns:

the latest IOException encountered in the print stream either on the underlying output stream or during a format conversion.
getException
()

Get latest exception.

public void
resetError()

Set the error back to null to be able to check for the next error using ICSVWriter#checkError().

public void
setResultService(ResultSetHelper
The ResultSetHelper
resultService
)

Sets the result service.

public void
writeAll(Iterable<String[]>
an Iterable of String[], with each String[] representing a line of the file.
allLines
,
boolean
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.
applyQuotesToAll
)

Writes iterable to a CSV file.

public default void
writeAll(List<String[]>
A List of String[] with each String[] representing a line of the file.
allLines
,
boolean
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.
applyQuotesToAll
)

Writes the entire list to a CSV file.

public default void
writeAll(Iterable<String[]>
an Iterable of String[], with each String[] representing a line of the file.
allLines
)

Writes iterable to a CSV file.

public default void
writeAll(List<String[]>
A List of String[] with each String[] representing a line of the file.
allLines
)

Writes the entire list to a CSV file.

public default int

Returns:

Number of lines written.
writeAll
(ResultSet
The result set to write
rs
,
boolean
True if you want column names in the output, false otherwise
includeColumnNames
)

Writes the entire ResultSet to a CSV file.

public default int

Returns:

Number of lines written - including header.
writeAll
(ResultSet
The Result set to write.
rs
,
boolean
Include the column names in the output.
includeColumnNames
,
boolean
Remove spaces from the data before writing.
trim
)

Writes the entire ResultSet to a CSV file.

public int

Returns:

Number of lines written - including header.
writeAll
(ResultSet
The Result set to write.
rs
,
boolean
Include the column names in the output.
includeColumnNames
,
boolean
Remove spaces from the data before writing.
trim
,
boolean
Whether all values should be quoted.
applyQuotesToAll
)

Writes the entire ResultSet to a CSV file.

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

Writes the next line to the file.

public default void
writeNext(String[]
A string array with each comma-separated element as a separate entry.
nextLine
)

Writes the next line to the file.

Inherited from java.io.Closeable:
close
Inherited from java.io.Flushable:
flush

Field Detail

DEFAULT_ESCAPE_CHARACTERback to summary
public static final char DEFAULT_ESCAPE_CHARACTER

The character used for escaping quotes.

DEFAULT_LINE_ENDback to summary
public static final String DEFAULT_LINE_END

Default line terminator.

DEFAULT_QUOTE_CHARACTERback to summary
public static final char DEFAULT_QUOTE_CHARACTER

The default quote character to use if none is supplied to the constructor.

DEFAULT_SEPARATORback to summary
public static final char DEFAULT_SEPARATOR

The default separator to use if none is supplied to the constructor.

INITIAL_STRING_SIZEback to summary
public static final int INITIAL_STRING_SIZE

Default buffer sizes

NO_ESCAPE_CHARACTERback to summary
public static final char NO_ESCAPE_CHARACTER

The escape constant to use when you wish to suppress all escaping.

NO_QUOTE_CHARACTERback to summary
public static final char NO_QUOTE_CHARACTER

The quote constant to use when you wish to suppress all quoting.

RFC4180_LINE_ENDback to summary
public static final String RFC4180_LINE_END

RFC 4180 compliant line terminator.

Method Detail

checkErrorback to summary
public boolean checkError()

Flushes the buffer and checks to see if the there has been an error in the printstream.

Returns:boolean

True if the print stream has encountered an error either on the underlying output stream or during a format conversion.

flushQuietlyback to summary
public default void flushQuietly()

Flushes the writer without throwing any exceptions.

getExceptionback to summary
public 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:IOException

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

resetErrorback to summary
public void resetError()

Set the error back to null to be able to check for the next error using ICSVWriter#checkError().

setResultServiceback to summary
public void setResultService(ResultSetHelper resultService)

Sets the result service.

Parameters
resultService:ResultSetHelper

The ResultSetHelper

writeAllback to summary
public void writeAll(Iterable<String[]> allLines, boolean applyQuotesToAll)

Writes iterable to a CSV file. The list is assumed to be a String[]

Parameters
allLines:Iterable<String[]>

an Iterable of String[], with each String[] representing a line of the file.

applyQuotesToAll:boolean

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.

writeAllback to summary
public 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:List<String[]>

A List of String[] with each String[] representing a line of the file.

applyQuotesToAll:boolean

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.

writeAllback to summary
public default void writeAll(Iterable<String[]> allLines)

Writes iterable to a CSV file. The list is assumed to be a String[]

Parameters
allLines:Iterable<String[]>

an Iterable of String[], with each String[] representing a line of the file.

writeAllback to summary
public default void writeAll(List<String[]> allLines)

Writes the entire list to a CSV file. The list is assumed to be a String[].

Parameters
allLines:List<String[]>

A List of String[] with each String[] representing a line of the file.

writeAllback to summary
public 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:ResultSet

The result set to write

includeColumnNames:boolean

True if you want column names in the output, false otherwise

Returns:int

Number of lines written.

Exceptions
SQLException:
Thrown by ResultSetHelper.getColumnValues()
IOException:
Thrown by ResultSetHelper.getColumnValues()
writeAllback to summary
public 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:ResultSet

The Result set to write.

includeColumnNames:boolean

Include the column names in the output.

trim:boolean

Remove spaces from the data before writing.

Returns:int

Number of lines written - including header.

Exceptions
SQLException:
Thrown by ResultSetHelper.getColumnValues()
IOException:
Thrown by ResultSetHelper.getColumnValues()
writeAllback to summary
public 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:ResultSet

The Result set to write.

includeColumnNames:boolean

Include the column names in the output.

trim:boolean

Remove spaces from the data before writing.

applyQuotesToAll:boolean

Whether all values should be quoted.

Returns:int

Number of lines written - including header.

Exceptions
SQLException:
Thrown by ResultSetHelper.getColumnValues()
IOException:
Thrown by ResultSetHelper.getColumnValues()
writeNextback to summary
public void writeNext(String[] nextLine, boolean applyQuotesToAll)

Writes the next line to the file.

Parameters
nextLine:String[]

A string array with each comma-separated element as a separate entry.

applyQuotesToAll:boolean

True if all values are to be quoted. False applies quotes only to values which contain the separator, escape, quote, or new line characters.

writeNextback to summary
public default void writeNext(String[] nextLine)

Writes the next line to the file.

Parameters
nextLine:String[]

A string array with each comma-separated element as a separate entry.