Package com.opencsv
Class CSVWriter
java.lang.Object
com.opencsv.AbstractCSVWriter
com.opencsv.CSVWriter
- All Implemented Interfaces:
ICSVWriter
,Closeable
,Flushable
,AutoCloseable
A very simple CSV writer released under a commercial-friendly license.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final char
protected final char
protected final char
Fields inherited from class com.opencsv.AbstractCSVWriter
exception, lineEnd, resultService, 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 -
Method Summary
Modifier and TypeMethodDescriptionprivate void
appendQuoteCharacterIfNeeded
(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters) protected boolean
checkCharactersToEscape
(char 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, char nextChar) Appends the character to the StringBuilder adding the escape character if needed.protected void
processLine
(String nextElement, Appendable appendable) Processes all the characters in a line.protected boolean
Checks to see if the line contains special characters.protected void
writeNext
(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) Writes the next line to the file.Methods inherited from class com.opencsv.AbstractCSVWriter
checkError, close, flush, getException, resetError, resultService, setResultService, writeAll, writeAll, writeColumnNames, writeNext
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 Details
-
separator
protected final char separator -
quotechar
protected final char quotechar -
escapechar
protected final char escapechar
-
-
Constructor Details
-
CSVWriter
Constructs CSVWriter using a comma for the separator.- Parameters:
writer
- The writer to an underlying CSV source.
-
CSVWriter
Constructs CSVWriter with supplied separator, quote char, escape char and line ending.- Parameters:
writer
- The writer to an underlying CSV source.separator
- The delimiter to use for separating entriesquotechar
- The character to use for quoted elementsescapechar
- The character to use for escaping quotechars or escapecharslineEnd
- The line feed terminator to use
-
-
Method Details
-
writeNext
protected void writeNext(String[] nextLine, boolean applyQuotesToAll, Appendable appendable) throws IOException Description copied from class:AbstractCSVWriter
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).- Specified by:
writeNext
in classAbstractCSVWriter
- 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.
-
appendQuoteCharacterIfNeeded
private void appendQuoteCharacterIfNeeded(boolean applyQuotesToAll, Appendable appendable, Boolean stringContainsSpecialCharacters) throws IOException - Throws:
IOException
-
stringContainsSpecialCharacters
Checks to see if the line contains special characters.- Parameters:
line
- Element of data to check for special characters.- Returns:
- True if the line contains the quote, escape, separator, newline, or return.
-
processLine
Processes all the characters in a line.- Parameters:
nextElement
- Element to process.appendable
- - Appendable holding the processed data.- Throws:
IOException
- - IOException thrown by the writer supplied to the CSVWriter
-
processCharacter
Appends the character to the StringBuilder adding the escape character if needed.- Parameters:
appendable
- - Appendable holding the processed data.nextChar
- Character to process- Throws:
IOException
- - IOException thrown by the writer supplied to the CSVWriter.
-
checkCharactersToEscape
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
- The next character to be written- Returns:
- Whether the character needs to be quoted or not
-