Package com.opencsv
Class AbstractCSVWriter
- java.lang.Object
-
- com.opencsv.AbstractCSVWriter
-
- All Implemented Interfaces:
ICSVWriter
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
- Direct Known Subclasses:
CSVParserWriter
,CSVWriter
public abstract class AbstractCSVWriter extends java.lang.Object implements ICSVWriter
The AbstractCSVWriter was created to prevent duplication of code between the CSVWriter and the CSVParserWriter classes.- Since:
- 4.2
-
-
Field Summary
Fields Modifier and Type Field Description protected java.io.IOException
exception
protected java.lang.String
lineEnd
protected ResultSetHelper
resultService
protected java.io.Writer
writer
-
Fields 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
Constructors Constructor Description AbstractCSVWriter(java.io.Writer writer, java.lang.String lineEnd)
Constructor to initialize the common values.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description boolean
checkError()
Flushes the buffer and checks to see if the there has been an error in the printstream.void
close()
void
flush()
java.io.IOException
getException()
Get latest exception.void
resetError()
Set the error back to null to be able to check for the next error usingICSVWriter.checkError()
.protected ResultSetHelper
resultService()
Lazy resultSetHelper creation.void
setResultService(ResultSetHelper resultService)
Sets the result service.void
writeAll(java.lang.Iterable<java.lang.String[]> allLines, boolean applyQuotesToAll)
Writes iterable to a CSV file.int
writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll)
Writes the entire ResultSet to a CSV file.protected void
writeColumnNames(java.sql.ResultSet rs, boolean applyQuotesToAll)
Writes the column names.void
writeNext(java.lang.String[] nextLine, boolean applyQuotesToAll)
Writes the next line to the file.protected abstract void
writeNext(java.lang.String[] nextLine, boolean applyQuotesToAll, java.lang.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, wait
-
Methods inherited from interface com.opencsv.ICSVWriter
flushQuietly, writeAll, writeAll, writeAll, writeAll, writeAll, writeNext
-
-
-
-
Field Detail
-
writer
protected final java.io.Writer writer
-
lineEnd
protected java.lang.String lineEnd
-
resultService
protected ResultSetHelper resultService
-
exception
protected volatile java.io.IOException exception
-
-
Method Detail
-
writeAll
public void writeAll(java.lang.Iterable<java.lang.String[]> allLines, boolean applyQuotesToAll)
Description copied from interface:ICSVWriter
Writes iterable to a CSV file. The list is assumed to be a String[]- Specified by:
writeAll
in 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
protected void writeColumnNames(java.sql.ResultSet rs, boolean applyQuotesToAll) throws java.sql.SQLException
Writes the column names.- Parameters:
rs
- ResultSet containing column names.applyQuotesToAll
- Whether all header names should be quoted.- Throws:
java.sql.SQLException
- Thrown byResultSetHelper.getColumnNames(java.sql.ResultSet)
-
writeAll
public int writeAll(java.sql.ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws java.sql.SQLException, java.io.IOException
Description copied from interface:ICSVWriter
Writes the entire ResultSet to a CSV file. The caller is responsible for closing the ResultSet.- Specified by:
writeAll
in 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:
java.sql.SQLException
- Thrown by ResultSetHelper.getColumnValues()java.io.IOException
- Thrown by ResultSetHelper.getColumnValues()
-
writeNext
public void writeNext(java.lang.String[] nextLine, boolean applyQuotesToAll)
Description copied from interface:ICSVWriter
Writes the next line to the file.- Specified by:
writeNext
in 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(java.lang.String[] nextLine, boolean applyQuotesToAll, java.lang.Appendable appendable) throws java.io.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:
java.io.IOException
- Exceptions thrown by the writer supplied to CSVWriter.
-
flush
public void flush() throws java.io.IOException
- Specified by:
flush
in interfacejava.io.Flushable
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Throws:
java.io.IOException
-
checkError
public boolean checkError()
Description copied from interface:ICSVWriter
Flushes the buffer and checks to see if the there has been an error in the printstream.- Specified by:
checkError
in interfaceICSVWriter
- Returns:
- True if the print stream has encountered an error either on the underlying output stream or during a format conversion.
-
getException
public java.io.IOException getException()
Description copied from interface:ICSVWriter
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.
- Specified by:
getException
in 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:ICSVWriter
Set the error back to null to be able to check for the next error usingICSVWriter.checkError()
.- Specified by:
resetError
in interfaceICSVWriter
-
setResultService
public void setResultService(ResultSetHelper resultService)
Description copied from interface:ICSVWriter
Sets the result service.- Specified by:
setResultService
in interfaceICSVWriter
- Parameters:
resultService
- The ResultSetHelper
-
resultService
protected ResultSetHelper resultService()
Lazy resultSetHelper creation.- Returns:
- Instance of resultSetHelper
-
-