Package com.opencsv
Class AbstractCSVWriter
java.lang.Object
com.opencsv.AbstractCSVWriter
- All Implemented Interfaces:
ICSVWriter,Closeable,Flushable,AutoCloseable
- Direct Known Subclasses:
CSVParserWriter,CSVWriter
The AbstractCSVWriter was created to prevent duplication of code between the CSVWriter and the
CSVParserWriter classes.
- Since:
- 4.2
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected IOExceptionprotected Stringprotected ResultSetHelperprotected final WriterFields inherited from interface com.opencsv.ICSVWriter
DEFAULT_ESCAPE_CHARACTER, DEFAULT_LINE_END, DEFAULT_QUOTE_CHARACTER, DEFAULT_SEPARATOR, INITIAL_STRING_SIZE, NO_ESCAPE_CHARACTER, NO_QUOTE_CHARACTER, RFC4180_LINE_END -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractCSVWriter(Writer writer, String lineEnd) Constructor to initialize the common values. -
Method Summary
Modifier and TypeMethodDescriptionbooleanFlushes the buffer and checks to see if the there has been an error in the printstream.voidclose()voidflush()Get latest exception.voidSet the error back to null to be able to check for the next error usingICSVWriter.checkError().protected ResultSetHelperLazy resultSetHelper creation.voidsetResultService(ResultSetHelper resultService) Sets the result service.voidWrites iterable to a CSV file.intWrites the entire ResultSet to a CSV file.protected voidwriteColumnNames(ResultSet rs, boolean applyQuotesToAll) Writes the column names.voidWrites the next line to the file.protected abstract voidwriteNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) Writes the next line to the file.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.opencsv.ICSVWriter
flushQuietly, writeAll, writeAll, writeAll, writeAll, writeAll, writeNext
-
Field Details
-
writer
-
lineEnd
-
resultService
-
exception
-
-
Constructor Details
-
AbstractCSVWriter
Constructor to initialize the common values.- Parameters:
writer- Writer used for output of csv data.lineEnd- String to append at end of data (either "\n" or "\r\n").
-
-
Method Details
-
writeAll
Description copied from interface:ICSVWriterWrites iterable to a CSV file. The list is assumed to be a String[]- Specified by:
writeAllin interfaceICSVWriter- 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.
-
writeColumnNames
Writes the column names.- Parameters:
rs- ResultSet containing column names.applyQuotesToAll- Whether all header names should be quoted.- Throws:
SQLException- Thrown byResultSetHelper.getColumnNames(java.sql.ResultSet)
-
writeAll
public int writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws SQLException, IOException Description copied from interface:ICSVWriterWrites the entire ResultSet to a CSV file. The caller is responsible for closing the ResultSet.- Specified by:
writeAllin interfaceICSVWriter- 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:
SQLException- Thrown by ResultSetHelper.getColumnValues()IOException- Thrown by ResultSetHelper.getColumnValues()
-
writeNext
Description copied from interface:ICSVWriterWrites the next line to the file.- Specified by:
writeNextin interfaceICSVWriter- 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
protected abstract void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws IOException Writes the next line to the file. This method is a fail-fast method that will throw the IOException of the writer supplied to the CSVWriter (if the Writer does not handle the exceptions itself like the PrintWriter class).- 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.appendable- Appendable used as buffer.- Throws:
IOException- Exceptions thrown by the writer supplied to CSVWriter.
-
flush
- Specified by:
flushin interfaceFlushable- Throws:
IOException
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
checkError
public boolean checkError()Description copied from interface:ICSVWriterFlushes the buffer and checks to see if the there has been an error in the printstream.- Specified by:
checkErrorin interfaceICSVWriter- Returns:
- True if the print stream has encountered an error either on the underlying output stream or during a format conversion.
-
getException
Description copied from interface:ICSVWriterGet 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.
- Specified by:
getExceptionin interfaceICSVWriter- Returns:
- the latest IOException encountered in the print stream either on the underlying output stream or during a format conversion.
-
resetError
public void resetError()Description copied from interface:ICSVWriterSet the error back to null to be able to check for the next error usingICSVWriter.checkError().- Specified by:
resetErrorin interfaceICSVWriter
-
setResultService
Description copied from interface:ICSVWriterSets the result service.- Specified by:
setResultServicein interfaceICSVWriter- Parameters:
resultService- The ResultSetHelper
-
resultService
Lazy resultSetHelper creation.- Returns:
- Instance of resultSetHelper
-