Top Description Fields Constructors Methods
com.opencsv

public Class CSVWriter

extends AbstractCSVWriter
Class Inheritance
Imports
java.io.IOException, .Writer

A very simple CSV writer released under a commercial-friendly license.
Author
Glen Smith

Field Summary

Modifier and TypeField and Description
protected final char
protected final char
protected final char
Inherited from com.opencsv.AbstractCSVWriter:
exception

Constructor Summary

AccessConstructor and Description
public
CSVWriter(Writer
The writer to an underlying CSV source.
writer
)

Constructs CSVWriter using a comma for the separator.

public
CSVWriter(Writer
The writer to an underlying CSV source.
writer
,
char
The delimiter to use for separating entries
separator
,
char
The character to use for quoted elements
quotechar
,
char
The character to use for escaping quotechars or escapechars
escapechar
,
String
The line feed terminator to use
lineEnd
)

Constructs CSVWriter with supplied separator, quote char, escape char and line ending.

Method Summary

Modifier and TypeMethod and Description
private void
appendQuoteCharacterIfNeeded(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters)

protected boolean

Returns:

Whether the character needs to be quoted or not
checkCharactersToEscape
(char
The next character to be written
nextChar
)

Checks whether the next character that is to be written out is a special character that must be quoted.

protected void
processCharacter(Appendable
- Appendable holding the processed data.
appendable
,
char
Character to process
nextChar
)

Appends the character to the StringBuilder adding the escape character if needed.

protected void
processLine(String
Element to process.
nextElement
,
Appendable
- Appendable holding the processed data.
appendable
)

Processes all the characters in a line.

protected boolean

Returns:

True if the line contains the quote, escape, separator, newline, or return.
stringContainsSpecialCharacters
(String
Element of data to check for special characters.
line
)

Checks to see if the line contains special characters.

protected void
writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable)

Implements abstract com.opencsv.AbstractCSVWriter.writeNext.

Writes the next line to the file.

Inherited from com.opencsv.AbstractCSVWriter:
checkErrorcloseflushgetExceptiongetLineEndgetWriterresetErrorresultServicesetResultServicewriteAllwriteAllwriteColumnNameswriteNext

Field Detail

escapecharback to summary
protected final char escapechar
quotecharback to summary
protected final char quotechar
separatorback to summary
protected final char separator

Constructor Detail

CSVWriterback to summary
public CSVWriter(Writer writer)

Constructs CSVWriter using a comma for the separator.

Parameters
writer:Writer

The writer to an underlying CSV source.

CSVWriterback to summary
public CSVWriter(Writer writer, char separator, char quotechar, char escapechar, String lineEnd)

Constructs CSVWriter with supplied separator, quote char, escape char and line ending.

Parameters
writer:Writer

The writer to an underlying CSV source.

separator:char

The delimiter to use for separating entries

quotechar:char

The character to use for quoted elements

escapechar:char

The character to use for escaping quotechars or escapechars

lineEnd:String

The line feed terminator to use

Method Detail

appendQuoteCharacterIfNeededback to summary
private void appendQuoteCharacterIfNeeded(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters) throws IOException
checkCharactersToEscapeback to summary
protected boolean checkCharactersToEscape(char nextChar)

Checks whether the next character that is to be written out is a special character that must be quoted. The quote character, escape charater, and separator are special characters.

Parameters
nextChar:char

The next character to be written

Returns:boolean

Whether the character needs to be quoted or not

processCharacterback to summary
protected void processCharacter(Appendable appendable, char nextChar) throws IOException

Appends the character to the StringBuilder adding the escape character if needed.

Parameters
appendable:Appendable

- Appendable holding the processed data.

nextChar:char

Character to process

Exceptions
IOException:
- IOException thrown by the writer supplied to the CSVWriter.
processLineback to summary
protected void processLine(String nextElement, Appendable appendable) throws IOException

Processes all the characters in a line.

Parameters
nextElement:String

Element to process.

appendable:Appendable

- Appendable holding the processed data.

Exceptions
IOException:
- IOException thrown by the writer supplied to the CSVWriter
stringContainsSpecialCharactersback to summary
protected boolean stringContainsSpecialCharacters(String line)

Checks to see if the line contains special characters.

Parameters
line:String

Element of data to check for special characters.

Returns:boolean

True if the line contains the quote, escape, separator, newline, or return.

writeNextback to summary
protected void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws IOException

Implements abstract com.opencsv.AbstractCSVWriter.writeNext.

Doc from com.opencsv.AbstractCSVWriter.writeNext.

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).

Annotations
@Override
Exceptions
IOException:
Exceptions thrown by the writer supplied to CSVWriter.