Package com.opencsv
Interface ICSVWriter
- All Superinterfaces:
AutoCloseable
,Closeable
,Flushable
- All Known Implementing Classes:
AbstractCSVWriter
,CSVParserWriter
,CSVWriter
This interface defines all the behavior of a csv writer class.
- Since:
- 4.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic 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 sizesstatic 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 TypeMethodDescriptionboolean
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 usingcheckError()
.void
setResultService
(ResultSetHelper resultService) Sets the result service.default void
Writes iterable to a CSV file.void
Writes iterable to a CSV file.default int
Writes the entire ResultSet to a CSV file.default int
Writes the entire ResultSet to a CSV file.int
Writes the entire ResultSet to a CSV file.default void
Writes the entire list to a CSV file.default void
Writes the entire list to a CSV file.default void
Writes the next line to the file.void
Writes the next line to the file.
-
Field Details
-
DEFAULT_LINE_END
Default line terminator.- See Also:
-
RFC4180_LINE_END
RFC 4180 compliant line terminator.- See Also:
-
INITIAL_STRING_SIZE
static final int INITIAL_STRING_SIZEDefault buffer sizes- See Also:
-
DEFAULT_ESCAPE_CHARACTER
static final char DEFAULT_ESCAPE_CHARACTERThe character used for escaping quotes.- See Also:
-
DEFAULT_SEPARATOR
static final char DEFAULT_SEPARATORThe default separator to use if none is supplied to the constructor.- See Also:
-
DEFAULT_QUOTE_CHARACTER
static final char DEFAULT_QUOTE_CHARACTERThe default quote character to use if none is supplied to the constructor.- See Also:
-
NO_QUOTE_CHARACTER
static final char NO_QUOTE_CHARACTERThe quote constant to use when you wish to suppress all quoting.- See Also:
-
NO_ESCAPE_CHARACTER
static final char NO_ESCAPE_CHARACTERThe escape constant to use when you wish to suppress all escaping.- See Also:
-
-
Method Details
-
writeAll
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
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
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
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
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 writeincludeColumnNames
- 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
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
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 usingcheckError()
. -
setResultService
Sets the result service.- Parameters:
resultService
- The ResultSetHelper
-
flushQuietly
default void flushQuietly()Flushes the writer without throwing any exceptions.
-