Package com.opencsv
Class CSVWriter
- java.lang.Object
-
- com.opencsv.AbstractCSVWriter
-
- com.opencsv.CSVWriter
-
- All Implemented Interfaces:
ICSVWriter
,java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class CSVWriter extends AbstractCSVWriter
A very simple CSV writer released under a commercial-friendly license.- Author:
- Glen Smith
-
-
Field Summary
Fields Modifier and Type Field Description protected char
escapechar
protected char
quotechar
protected char
separator
-
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 Constructor Description CSVWriter(java.io.Writer writer)
Constructs CSVWriter using a comma for the separator.CSVWriter(java.io.Writer writer, char separator, char quotechar, char escapechar, java.lang.String lineEnd)
Constructs CSVWriter with supplied separator, quote char, escape char and line ending.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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(java.lang.Appendable appendable, char nextChar)
Appends the character to the StringBuilder adding the escape character if needed.protected void
processLine(java.lang.String nextElement, java.lang.Appendable appendable)
Processes all the characters in a line.protected boolean
stringContainsSpecialCharacters(java.lang.String line)
Checks to see if the line contains special characters.protected void
writeNext(java.lang.String[] nextLine, boolean applyQuotesToAll, java.lang.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
-
-
-
-
Constructor Detail
-
CSVWriter
public CSVWriter(java.io.Writer writer)
Constructs CSVWriter using a comma for the separator.- Parameters:
writer
- The writer to an underlying CSV source.
-
CSVWriter
public CSVWriter(java.io.Writer writer, char separator, char quotechar, char escapechar, java.lang.String lineEnd)
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 Detail
-
writeNext
protected void writeNext(java.lang.String[] nextLine, boolean applyQuotesToAll, java.lang.Appendable appendable) throws java.io.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:
java.io.IOException
- Exceptions thrown by the writer supplied to CSVWriter.
-
stringContainsSpecialCharacters
protected boolean stringContainsSpecialCharacters(java.lang.String line)
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
protected void processLine(java.lang.String nextElement, java.lang.Appendable appendable) throws java.io.IOException
Processes all the characters in a line.- Parameters:
nextElement
- Element to process.appendable
- - Appendable holding the processed data.- Throws:
java.io.IOException
- - IOException thrown by the writer supplied to the CSVWriter
-
processCharacter
protected void processCharacter(java.lang.Appendable appendable, char nextChar) throws java.io.IOException
Appends the character to the StringBuilder adding the escape character if needed.- Parameters:
appendable
- - Appendable holding the processed data.nextChar
- Character to process- Throws:
java.io.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
-
-