Class CSVWriter
Writes rows of values as CSV, the counterpart of CSVParser.
A value is quoted when it has to be: when it contains the separator or any other common one (comma, semicolon, tab, bar), so a reader that detects the separator can't split it, the quote character or a line break, or begins or ends with a space. The quote character inside a quoted value is written twice.
CSVWriter csv = new CSVWriter();
String text = csv.toString(new String[][] {
{"Name", "Notes"},
{"Doe, John", "said \"hello\""}
});
Log.p(text);
Spreadsheets open a .csv file with the separator of the machine's locale and, without a
byte order mark, in a legacy code page. setWriteByteOrderMark(boolean) makes them read
UTF-8, and setWriteSeparatorHint(boolean) writes the sep= first line that names the
separator. Other consumers of CSV read both as data, which is why neither is on by default.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionThe text written after every row.charThe character that encloses a quoted value.charThe separator written between the values of a row.booleanWhether values a spreadsheet would run as a formula are neutralized.booleanWhether every value is quoted.booleanWhetherwrite(OutputStream, String[][])starts the file with a byte order mark.booleanWhether asep=line is written before the rows.voidsetEscapeFormulas(boolean escapeFormulas) Neutralizes values a spreadsheet would run as a formula.voidsetLineEnding(String lineEnding) Changes the text written after every row.voidsetQuoteAll(boolean quoteAll) Quotes every value, including those that do not need it.voidsetQuoteChar(char quoteChar) Changes the character that encloses a quoted value.voidsetWriteByteOrderMark(boolean writeByteOrderMark) Starts a file written bywrite(OutputStream, String[][])with the UTF-8 byte order mark.voidsetWriteSeparatorHint(boolean writeSeparatorHint) Writes a first line of the formsep=,that tells a spreadsheet which separator the file uses, whatever the locale of the machine.Returns rows as CSV text.voidwrite(OutputStream out, String[][] rows) Writes rows as UTF-8.voidWrites rows.voidwriteHeader(Writer out) Writes thesep=line ifsetWriteSeparatorHint(boolean)asked for one.voidWrites one row followed by the line ending, for output that is produced row by row.
-
Constructor Details
-
CSVWriter
public CSVWriter()Creates a writer with the comma separator. -
CSVWriter
public CSVWriter(char separatorChar) Creates a writer with a custom separator.
Parameters
separatorChar: the separator, a tab for tab separated values
Throws
IllegalArgumentException: for a line break or a double quote, which no reader could tell from a row end or a quote
-
-
Method Details
-
getSeparatorChar
public char getSeparatorChar()The separator written between the values of a row.
Returns
the separator character
-
getQuoteChar
public char getQuoteChar()The character that encloses a quoted value.
Returns
the quote character
-
setQuoteChar
public void setQuoteChar(char quoteChar) Changes the character that encloses a quoted value.
Parameters
quoteChar: the quote character, a double quote by default
Throws
IllegalArgumentException: for the separator or a line break
-
getLineEnding
The text written after every row.
Returns
the line ending
-
setLineEnding
Changes the text written after every row. The default is CR LF, which is what the CSV specification (RFC 4180) asks for and what every reader accepts.
Parameters
lineEnding: the line ending
Throws
IllegalArgumentException: for anything but CR LF, LF or CR
-
isQuoteAll
public boolean isQuoteAll()Whether every value is quoted.
Returns
true if every value is quoted
-
setQuoteAll
public void setQuoteAll(boolean quoteAll) Quotes every value, including those that do not need it.
Parameters
quoteAll: true to quote every value
-
isEscapeFormulas
public boolean isEscapeFormulas()Whether values a spreadsheet would run as a formula are neutralized.
Returns
true if such values are prefixed
-
setEscapeFormulas
public void setEscapeFormulas(boolean escapeFormulas) Neutralizes values a spreadsheet would run as a formula. A spreadsheet that opens a CSV file treats a value starting with
=,+,-or@as a formula, and a formula can send the rest of the sheet to a web address. When the values come from people other than the one who opens the file, turn this on: such a value is written with a leading apostrophe, which a spreadsheet shows as plain text. A value that is a number, such as-12.5, is left as it is.Parameters
escapeFormulas: true to prefix such values
-
isWriteByteOrderMark
public boolean isWriteByteOrderMark()Whether
write(OutputStream, String[][])starts the file with a byte order mark.Returns
true if the mark is written
-
setWriteByteOrderMark
public void setWriteByteOrderMark(boolean writeByteOrderMark) Starts a file written by
write(OutputStream, String[][])with the UTF-8 byte order mark. Spreadsheets need it to read anything outside ASCII correctly.Parameters
writeByteOrderMark: true to write the mark
-
isWriteSeparatorHint
public boolean isWriteSeparatorHint()Whether a
sep=line is written before the rows.Returns
true if the line is written
-
setWriteSeparatorHint
public void setWriteSeparatorHint(boolean writeSeparatorHint) Writes a first line of the form
sep=,that tells a spreadsheet which separator the file uses, whatever the locale of the machine.CSVParserreads that line when separator detection is on.Parameters
writeSeparatorHint: true to write the line
-
write
Writes rows as UTF-8. The stream is flushed and left open.
Parameters
-
out: the destination -
rows: the rows, each an array of values; a null value is written as an empty one
Throws
java.io.IOException: if the stream cannot be written
- Throws:
IOException
-
-
write
Writes rows. The writer is left open.
Parameters
-
out: the destination -
rows: the rows, each an array of values; a null value is written as an empty one
Throws
java.io.IOException: if the writer fails
- Throws:
IOException
-
-
writeHeader
Writes the
sep=line ifsetWriteSeparatorHint(boolean)asked for one. Call it once, before the firstwriteRow(Writer, String...), when writing row by row;write(Writer, String[][])calls it itself.Parameters
out: the destination
Throws
java.io.IOException: if the writer fails
- Throws:
IOException
-
writeRow
Writes one row followed by the line ending, for output that is produced row by row.
Parameters
-
out: the destination -
cells: the values of the row; a null value is written as an empty one
Throws
java.io.IOException: if the writer fails
- Throws:
IOException
-
-
toString
-