Package com.helger.commons.csv
Class CSVWriter
java.lang.Object
com.helger.commons.csv.CSVWriter
- All Implemented Interfaces:
Closeable,Flushable,AutoCloseable
A very simple CSV writer released under a commercial-friendly license.
- Author:
- Glen Smith, Philip Helger
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final booleanBy default the file ends with a new-line.static final StringThe default line delimiter to be used.static final booleanBy default all text fields are always quoted.static final charThe escape constant to use when you wish to suppress all escaping.static final charThe quote constant to use when you wish to suppress all quoting. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanChecks to see if the there has been an error in the printstream.voidclose()Close the underlying stream writer flushing any buffered content.voidflush()Flush underlying stream to writer.voidflushes the writer without throwing any exceptions.charprotected StringBuildergetEscapedText(String sNextElement) Processes all the characters in a line.charcharintbooleanbooleansetApplyQuotesToAll(boolean bApplyQuotesToAll) Set whether all cells should be quoted by default or not.setAvoidFinalLineEnd(boolean bAvoidFinalLineEnd) Set whether the CSV file should end with a new line or not.setEscapeChar(char cEscapeChar) Sets the character to use for escaping a separator or quote.setLineEnd(String sLineEnd) Set the line delimiting string.setQuoteChar(char cQuoteChar) Sets the character to use for quoted elements.setSeparatorChar(char cSeparator) Sets the delimiter to use for separating entries.protected booleanchecks to see if the line contains special characters.voidWrites the entire list to a CSV file using the default quoting setting.voidWrites the entire list to a CSV file.voidWrites the next line to the file using the default quoting settings.voidWrites the next line to the file.voidWrites the next line to the file using the default quoting settings.voidWrites the next line to the file.voidWrites the next line to the file using the default quoting setting.voidWrites the next line to the file.voidWrites the next line to the file.
-
Field Details
-
NO_QUOTE_CHARACTER
public static final char NO_QUOTE_CHARACTERThe quote constant to use when you wish to suppress all quoting.- See Also:
-
NO_ESCAPE_CHARACTER
public static final char NO_ESCAPE_CHARACTERThe escape constant to use when you wish to suppress all escaping.- See Also:
-
DEFAULT_LINE_END
The default line delimiter to be used.- See Also:
-
DEFAULT_QUOTE_ALL
public static final boolean DEFAULT_QUOTE_ALLBy default all text fields are always quoted.- See Also:
-
DEFAULT_AVOID_FINAL_LINE_END
public static final boolean DEFAULT_AVOID_FINAL_LINE_ENDBy default the file ends with a new-line.- See Also:
-
-
Constructor Details
-
CSVWriter
ConstructsCSVWriterwith all default settings.- Parameters:
aWriter- the writer to an underlying CSV source. May not benull.
-
-
Method Details
-
getSeparatorChar
public char getSeparatorChar()- Returns:
- The default separator for this writer.
-
setSeparatorChar
Sets the delimiter to use for separating entries.- Parameters:
cSeparator- the delimiter to use for separating entries- Returns:
- this
-
getQuoteChar
public char getQuoteChar()- Returns:
- The default quotation character for this writer.
-
setQuoteChar
Sets the character to use for quoted elements.- Parameters:
cQuoteChar- the character to use for quoted element.- Returns:
- this
-
getEscapeChar
public char getEscapeChar()- Returns:
- The default escape character for this writer.
-
setEscapeChar
Sets the character to use for escaping a separator or quote.- Parameters:
cEscapeChar- the character to use for escaping a separator or quote.- Returns:
- this
-
getLineEnd
- Returns:
- the line delimiting string. Neither
nullnor empty.
-
setLineEnd
Set the line delimiting string.- Parameters:
sLineEnd- The line end. May neither benullnor empty.- Returns:
- this
-
isAvoidFinalLineEnd
public boolean isAvoidFinalLineEnd()- Returns:
trueif the file should not end with a new-line,falseotherwise.- Since:
- 8.6.6
-
setAvoidFinalLineEnd
Set whether the CSV file should end with a new line or not.- Parameters:
bAvoidFinalLineEnd-trueto avoid the CSV file ending with a new line.- Returns:
- this for chaining
- Since:
- 8.6.6
-
isApplyQuotesToAll
public boolean isApplyQuotesToAll()- Returns:
trueif all cells should always be quoted,falseotherwise. The default is true.- Since:
- 8.6.6
-
setApplyQuotesToAll
Set whether all cells should be quoted by default or not.- Parameters:
bApplyQuotesToAll-trueto quote all cells,falseto quote only the ones where it is necessary- Returns:
- this for chaining
- Since:
- 8.6.6
-
writeAll
Writes the entire list to a CSV file.- Parameters:
aAllLines- a List of List of String, with each List of String representing a line of the file.bApplyQuotesToAll-trueif all values are to be quoted.falseif quotes only to be applied to values which contain the separator, escape, quote or new line characters.
-
writeAll
Writes the entire list to a CSV file using the default quoting setting.- Parameters:
aAllLines- a List of List of String, with each List of String representing a line of the file.- See Also:
-
writeNext
Writes the next line to the file.- Parameters:
aNextLine- A collection of Strings where each entry represents a single cell.bApplyQuotesToAll-trueif all values are to be quoted.falseapplies quotes only to values which contain the separator, escape, quote or new line characters.
-
writeNext
Writes the next line to the file.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.bApplyQuotesToAll-trueif all values are to be quoted.falseapplies quotes only to values which contain the separator, escape, quote or new line characters.
-
writeNext
Writes the next line to the file using the default quoting settings.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.- See Also:
-
writeNext
Writes the next line to the file.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.bApplyQuotesToAll-trueif all values are to be quoted.falseapplies quotes only to values which contain the separator, escape, quote or new line characters.
-
writeNext
Writes the next line to the file using the default quoting settings.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.- See Also:
-
writeNext
public void writeNext(@Nullable String[] aNextLine, @Nonnegative int nOfs, @Nonnegative int nLength, boolean bApplyQuotesToAll) Writes the next line to the file.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.nOfs- Array Offset. Must be ≥ 0.nLength- Array Length. Must be ≥ 0.bApplyQuotesToAll-trueif all values are to be quoted.falseapplies quotes only to values which contain the separator, escape, quote or new line characters.
-
writeNext
public void writeNext(@Nullable String[] aNextLine, @Nonnegative int nOfs, @Nonnegative int nLength) Writes the next line to the file using the default quoting setting.- Parameters:
aNextLine- a string array with each comma-separated element as a separate entry.nOfs- Array Offset. Must be ≥ 0.nLength- Array Length. Must be ≥ 0.- See Also:
-
stringContainsSpecialCharacters
checks to see if the line contains special characters.- Parameters:
sLine- - element of data to check for special characters.- Returns:
- true if the line contains the quote, escape, separator, newline or return.
-
getEscapedText
Processes all the characters in a line.- Parameters:
sNextElement- element to process.- Returns:
- a StringBuilder with the elements data.
-
flush
Flush underlying stream to writer.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if bad things happen
-
close
Close the underlying stream writer flushing any buffered content.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if bad things happen
-
checkError
public boolean checkError()Checks to see if the there has been an error in the printstream.- Returns:
trueif the print stream has encountered an error, either on the underlying output stream or during a format conversion.
-
getWrittenLines
- Returns:
- The number of written lines. Always ≥ 0.
- Since:
- 8.6.6
-
flushQuietly
public void flushQuietly()flushes the writer without throwing any exceptions.
-