Package com.helger.commons.csv
Class CSVWriter
- java.lang.Object
-
- com.helger.commons.csv.CSVWriter
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEFAULT_AVOID_FINAL_LINE_ENDBy default the file ends with a new-line.static StringDEFAULT_LINE_ENDThe default line delimiter to be used.static booleanDEFAULT_QUOTE_ALLBy default all text fields are always quoted.static charNO_ESCAPE_CHARACTERThe escape constant to use when you wish to suppress all escaping.static charNO_QUOTE_CHARACTERThe quote constant to use when you wish to suppress all quoting.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancheckError()Checks 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.voidflushQuietly()flushes the writer without throwing any exceptions.chargetEscapeChar()protected StringBuildergetEscapedText(String sNextElement)Processes all the characters in a line.StringgetLineEnd()chargetQuoteChar()chargetSeparatorChar()intgetWrittenLines()booleanisApplyQuotesToAll()booleanisAvoidFinalLineEnd()CSVWritersetApplyQuotesToAll(boolean bApplyQuotesToAll)Set whether all cells should be quoted by default or not.CSVWritersetAvoidFinalLineEnd(boolean bAvoidFinalLineEnd)Set whether the CSV file should end with a new line or not.CSVWritersetEscapeChar(char cEscapeChar)Sets the character to use for escaping a separator or quote.CSVWritersetLineEnd(String sLineEnd)Set the line delimiting string.CSVWritersetQuoteChar(char cQuoteChar)Sets the character to use for quoted elements.CSVWritersetSeparatorChar(char cSeparator)Sets the delimiter to use for separating entries.protected booleanstringContainsSpecialCharacters(String sLine)checks to see if the line contains special characters.voidwriteAll(List<? extends List<String>> aAllLines)Writes the entire list to a CSV file using the default quoting setting.voidwriteAll(List<? extends List<String>> aAllLines, boolean bApplyQuotesToAll)Writes the entire list to a CSV file.voidwriteNext(Iterable<String> aNextLine)Writes the next line to the file using the default quoting settings.voidwriteNext(Iterable<String> aNextLine, boolean bApplyQuotesToAll)Writes the next line to the file.voidwriteNext(String... aNextLine)Writes the next line to the file using the default quoting settings.voidwriteNext(String[] aNextLine, boolean bApplyQuotesToAll)Writes the next line to the file.voidwriteNext(String[] aNextLine, int nOfs, int nLength)Writes the next line to the file using the default quoting setting.voidwriteNext(String[] aNextLine, int nOfs, int nLength, boolean bApplyQuotesToAll)Writes the next line to the file.voidwriteNext(Iterator<String> aNextLine, boolean bApplyQuotesToAll)Writes the next line to the file.
-
-
-
Field Detail
-
NO_QUOTE_CHARACTER
public static final char NO_QUOTE_CHARACTER
The quote constant to use when you wish to suppress all quoting.- See Also:
- Constant Field Values
-
NO_ESCAPE_CHARACTER
public static final char NO_ESCAPE_CHARACTER
The escape constant to use when you wish to suppress all escaping.- See Also:
- Constant Field Values
-
DEFAULT_LINE_END
public static final String DEFAULT_LINE_END
The default line delimiter to be used.- See Also:
- Constant Field Values
-
DEFAULT_QUOTE_ALL
public static final boolean DEFAULT_QUOTE_ALL
By default all text fields are always quoted.- See Also:
- Constant Field Values
-
DEFAULT_AVOID_FINAL_LINE_END
public static final boolean DEFAULT_AVOID_FINAL_LINE_END
By default the file ends with a new-line.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CSVWriter
public CSVWriter(@Nonnull @WillCloseWhenClosed Writer aWriter)
ConstructsCSVWriterwith all default settings.- Parameters:
aWriter- the writer to an underlying CSV source. May not benull.
-
-
Method Detail
-
getSeparatorChar
public char getSeparatorChar()
- Returns:
- The default separator for this writer.
-
setSeparatorChar
@Nonnull public CSVWriter setSeparatorChar(char cSeparator)
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
@Nonnull public CSVWriter setQuoteChar(char cQuoteChar)
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
@Nonnull public CSVWriter setEscapeChar(char cEscapeChar)
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
@Nonnull @Nonempty public String getLineEnd()
- Returns:
- the line delimiting string. Neither
nullnor empty.
-
setLineEnd
@Nonnull public CSVWriter setLineEnd(@Nonnull @Nonempty String sLineEnd)
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
@Nonnull public CSVWriter setAvoidFinalLineEnd(boolean bAvoidFinalLineEnd)
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
@Nonnull public CSVWriter setApplyQuotesToAll(boolean bApplyQuotesToAll)
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
public void writeAll(@Nonnull List<? extends List<String>> aAllLines, boolean bApplyQuotesToAll)
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
public void writeAll(@Nonnull List<? extends List<String>> aAllLines)
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:
isApplyQuotesToAll()
-
writeNext
public void writeNext(@Nullable Iterator<String> aNextLine, boolean bApplyQuotesToAll)
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
public void writeNext(@Nullable Iterable<String> aNextLine, boolean bApplyQuotesToAll)
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
public void writeNext(@Nullable Iterable<String> aNextLine)
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:
isApplyQuotesToAll()
-
writeNext
public void writeNext(@Nullable String[] aNextLine, boolean bApplyQuotesToAll)
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
public void writeNext(@Nullable String... aNextLine)
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:
isApplyQuotesToAll()
-
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:
isApplyQuotesToAll()
-
stringContainsSpecialCharacters
protected boolean stringContainsSpecialCharacters(@Nonnull String sLine)
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
@Nonnull protected StringBuilder getEscapedText(@Nonnull String sNextElement)
Processes all the characters in a line.- Parameters:
sNextElement- element to process.- Returns:
- a StringBuilder with the elements data.
-
flush
public void flush() throws IOExceptionFlush underlying stream to writer.- Specified by:
flushin interfaceFlushable- Throws:
IOException- if bad things happen
-
close
public void close() throws IOExceptionClose 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
@Nonnegative public int getWrittenLines()
- Returns:
- The number of written lines. Always ≥ 0.
- Since:
- 8.6.6
-
flushQuietly
public void flushQuietly()
flushes the writer without throwing any exceptions.
-
-