Top Description Fields Constructors Methods
com.opencsv

public abstract Class AbstractCSVWriter

extends Object
implements ICSVWriter
Class Inheritance
All Implemented Interfaces
com.opencsv.ICSVWriter, java.io.Flushable, java.io.Closeable, java.lang.AutoCloseable
Known Direct Subclasses
com.opencsv.CSVParserWriter, com.opencsv.CSVWriter
Imports
java.io.IOException, .PrintWriter, .Writer, java.sql.ResultSet, .SQLException

The AbstractCSVWriter was created to prevent duplication of code between the CSVWriter and the CSVParserWriter classes.
Since
4.2

Field Summary

Modifier and TypeField and Description
protected volatile IOException
private final String
private ResultSetHelper
private final Writer

Constructor Summary

AccessConstructor and Description
protected
AbstractCSVWriter(Writer
Writer used for output of csv data.
writer
,
String
String to append at end of data (either "\n" or "\r\n").
lineEnd
)

Constructor to initialize the common values.

Method Summary

Modifier and TypeMethod and Description
public boolean
checkError()

Implements com.opencsv.ICSVWriter.checkError.

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

public void
close()

Implements java.io.Closeable.close.

Closes this stream and releases any system resources associated with it.

public void
flush()

Implements java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

public IOException
getException()

Implements com.opencsv.ICSVWriter.getException.

Get latest exception.

protected final String

Returns:

The line ending sequence, either "\n" or "\r\n".
getLineEnd
()

Retrieves the line ending sequence used when writing CSV data.

protected final Writer

Returns:

The Writer instance used for writing CSV content.
getWriter
()

Provides access to the writer used for outputting CSV data.

public void
resetError()

Implements com.opencsv.ICSVWriter.resetError.

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

protected ResultSetHelper

Returns:

Instance of resultSetHelper
resultService
()

Lazy resultSetHelper creation.

public void
setResultService(ResultSetHelper resultService)

Implements com.opencsv.ICSVWriter.setResultService.

Sets the result service.

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

Implements com.opencsv.ICSVWriter.writeAll.

Writes iterable to a CSV file.

public int
writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll)

Implements com.opencsv.ICSVWriter.writeAll.

Writes the entire ResultSet to a CSV file.

protected void
writeColumnNames(ResultSet
ResultSet containing column names.
rs
,
boolean
Whether all header names should be quoted.
applyQuotesToAll
)

Writes the column names.

public void
writeNext(String[] nextLine, boolean applyQuotesToAll)

Implements com.opencsv.ICSVWriter.writeNext.

Writes the next line to the file.

protected abstract 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
,
Appendable
Appendable used as buffer.
appendable
)

Writes the next line to the file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

exceptionback to summary
protected volatile IOException exception
lineEndback to summary
private final String lineEnd
resultServiceback to summary
private ResultSetHelper resultService
writerback to summary
private final Writer writer

Constructor Detail

AbstractCSVWriterback to summary
protected AbstractCSVWriter(Writer writer, String lineEnd)

Constructor to initialize the common values.

Parameters
writer:Writer

Writer used for output of csv data.

lineEnd:String

String to append at end of data (either "\n" or "\r\n").

Method Detail

checkErrorback to summary
public boolean checkError()

Implements com.opencsv.ICSVWriter.checkError.

Doc from com.opencsv.ICSVWriter.checkError.

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

Annotations
@Override
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.close.

Doc from java.io.Closeable.close.

Closes this stream and releases any system resources associated with it. If the stream is already closed then invoking this method has no effect.

As noted in AutoCloseable#close(), cases where the close may fail require careful attention. It is strongly advised to relinquish the underlying resources and to internally mark the Closeable as closed, prior to throwing the IOException.

Annotations
@Override
Exceptions
IOException:
if an I/O error occurs
flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.flush.

Doc from java.io.Flushable.flush.

Flushes this stream by writing any buffered output to the underlying stream.

Annotations
@Override
Exceptions
IOException:
If an I/O error occurs
getExceptionback to summary
public IOException getException()

Implements com.opencsv.ICSVWriter.getException.

Doc from com.opencsv.ICSVWriter.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.

Annotations
@Override
getLineEndback to summary
protected final String getLineEnd()

Retrieves the line ending sequence used when writing CSV data.

Returns:String

The line ending sequence, either "\n" or "\r\n".

getWriterback to summary
protected final Writer getWriter()

Provides access to the writer used for outputting CSV data.

Returns:Writer

The Writer instance used for writing CSV content.

resetErrorback to summary
public void resetError()

Implements com.opencsv.ICSVWriter.resetError.

Doc from com.opencsv.ICSVWriter.resetError.

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

Annotations
@Override
resultServiceback to summary
protected ResultSetHelper resultService()

Lazy resultSetHelper creation.

Returns:ResultSetHelper

Instance of resultSetHelper

setResultServiceback to summary
public void setResultService(ResultSetHelper resultService)

Implements com.opencsv.ICSVWriter.setResultService.

Doc from com.opencsv.ICSVWriter.setResultService.

Sets the result service.

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

Implements com.opencsv.ICSVWriter.writeAll.

Doc from com.opencsv.ICSVWriter.writeAll.

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

Annotations
@Override
writeAllback to summary
public int writeAll(ResultSet rs, boolean includeColumnNames, boolean trim, boolean applyQuotesToAll) throws SQLException, IOException

Implements com.opencsv.ICSVWriter.writeAll.

Doc from com.opencsv.ICSVWriter.writeAll.

Writes the entire ResultSet to a CSV file. The caller is responsible for closing the ResultSet.

Annotations
@Override
Exceptions
SQLException:
Thrown by ResultSetHelper.getColumnValues()
IOException:
Thrown by ResultSetHelper.getColumnValues()
writeColumnNamesback to summary
protected void writeColumnNames(ResultSet rs, boolean applyQuotesToAll) throws SQLException

Writes the column names.

Parameters
rs:ResultSet

ResultSet containing column names.

applyQuotesToAll:boolean

Whether all header names should be quoted.

Exceptions
SQLException:
Thrown by ResultSetHelper#getColumnNames(java.sql.ResultSet)
writeNextback to summary
public void writeNext(String[] nextLine, boolean applyQuotesToAll)

Implements com.opencsv.ICSVWriter.writeNext.

Doc from com.opencsv.ICSVWriter.writeNext.

Writes the next line to the file.

Annotations
@Override
writeNextback to summary
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: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.

appendable:Appendable

Appendable used as buffer.

Exceptions
IOException:
Exceptions thrown by the writer supplied to CSVWriter.