Package com.opencsv
Interface ICSVWriter
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable,java.io.Flushable
- All Known Implementing Classes:
AbstractCSVWriter,CSVParserWriter,CSVWriter
public interface ICSVWriter extends java.io.Closeable, java.io.FlushableThis interface defines all the behavior of a csv writer class.- Since:
- 4.2
-
-
Field Summary
Fields Modifier and Type Field Description static charDEFAULT_ESCAPE_CHARACTERThe character used for escaping quotes.static java.lang.StringDEFAULT_LINE_ENDDefault line terminator.static charDEFAULT_QUOTE_CHARACTERThe default quote character to use if none is supplied to the constructor.static charDEFAULT_SEPARATORThe default separator to use if none is supplied to the constructor.static intINITIAL_STRING_SIZEDefault buffer sizesstatic charNO_ESCAPE_CHARACTERThe escape constant to use when you wish to suppress all escaping.static charNO_QUOTE_CHARACTERThe quote constant to use when you wish to suppress all quoting.static java.lang.StringRFC4180_LINE_ENDRFC 4180 compliant line terminator.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description booleancheckError()Flushes the buffer and checks to see if the there has been an error in the printstream.default voidflushQuietly()Flushes the writer without throwing any exceptions.java.io.IOExceptiongetException()Get latest exception.voidresetError()Set the error back to null to be able to check for the next error usingcheckError().voidsetResultService(ResultSetHelper resultService)Sets the result service.default voidwriteAll(java.lang.Iterable<java.lang.String[]> allLines)Writes iterable to a CSV file.voidwriteAll(java.lang.Iterable<java.lang.String[]> allLines, boolean applyQuotesToAll)Writes iterable to a CSV file.default intwriteAll(java.sql.ResultSet rs, boolean includeColumnNames)Writes the entire ResultSet to a CSV file.default intwriteAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim)Writes the entire ResultSet to a CSV file.intwriteAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll)Writes the entire ResultSet to a CSV file.default voidwriteAll(java.util.List<java.lang.String[]> allLines)Writes the entire list to a CSV file.default voidwriteAll(java.util.List<java.lang.String[]> allLines, boolean applyQuotesToAll)Writes the entire list to a CSV file.default voidwriteNext(java.lang.String[] nextLine)Writes the next line to the file.voidwriteNext(java.lang.String[] nextLine, boolean applyQuotesToAll)Writes the next line to the file.
-
-
-
Field Detail
-
DEFAULT_LINE_END
static final java.lang.String DEFAULT_LINE_END
Default line terminator.- See Also:
- Constant Field Values
-
RFC4180_LINE_END
static final java.lang.String RFC4180_LINE_END
RFC 4180 compliant line terminator.- See Also:
- Constant Field Values
-
INITIAL_STRING_SIZE
static final int INITIAL_STRING_SIZE
Default buffer sizes- See Also:
- Constant Field Values
-
DEFAULT_ESCAPE_CHARACTER
static final char DEFAULT_ESCAPE_CHARACTER
The character used for escaping quotes.- See Also:
- Constant Field Values
-
DEFAULT_SEPARATOR
static final char DEFAULT_SEPARATOR
The default separator to use if none is supplied to the constructor.- See Also:
- Constant Field Values
-
DEFAULT_QUOTE_CHARACTER
static final char DEFAULT_QUOTE_CHARACTER
The default quote character to use if none is supplied to the constructor.- See Also:
- Constant Field Values
-
NO_QUOTE_CHARACTER
static final char NO_QUOTE_CHARACTER
The quote constant to use when you wish to suppress all quoting.- See Also:
- Constant Field Values
-
NO_ESCAPE_CHARACTER
static final char NO_ESCAPE_CHARACTER
The escape constant to use when you wish to suppress all escaping.- See Also:
- Constant Field Values
-
-
Method Detail
-
writeAll
void writeAll(java.lang.Iterable<java.lang.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(java.util.List<java.lang.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(java.lang.Iterable<java.lang.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(java.util.List<java.lang.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(java.sql.ResultSet rs, boolean includeColumnNames) throws java.sql.SQLException, java.io.IOExceptionWrites 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:
java.io.IOException- Thrown by ResultSetHelper.getColumnValues()java.sql.SQLException- Thrown by ResultSetHelper.getColumnValues()
-
writeAll
default int writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim) throws java.sql.SQLException, java.io.IOExceptionWrites 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:
java.io.IOException- Thrown by ResultSetHelper.getColumnValues()java.sql.SQLException- Thrown by ResultSetHelper.getColumnValues()
-
writeAll
int writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws java.sql.SQLException, java.io.IOExceptionWrites 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:
java.io.IOException- Thrown by ResultSetHelper.getColumnValues()java.sql.SQLException- Thrown by ResultSetHelper.getColumnValues()
-
writeNext
void writeNext(java.lang.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(java.lang.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
java.io.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
void setResultService(ResultSetHelper resultService)
Sets the result service.- Parameters:
resultService- The ResultSetHelper
-
flushQuietly
default void flushQuietly()
Flushes the writer without throwing any exceptions.
-
-