Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public class CSVWriter
- Object
- 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.
Constructors
public CSVWriter() | Creates a writer with the comma separator. |
public CSVWriter(char separatorChar) | Creates a writer with a custom separator. |
Methods
Inherited methods
Constructor details
CSVWriter
public CSVWriter()CSVWriter
public CSVWriter(char separatorChar)Parameters
separatorCharchar- 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()Returns
getQuoteChar
public char getQuoteChar()Returns
setQuoteChar
public void setQuoteChar(char quoteChar)Parameters
quoteCharchar- the quote character, a double quote by default
Throws
IllegalArgumentException- for the separator or a line break
getLineEnding
public String getLineEnding()Returns
setLineEnding
public void setLineEnding(String lineEnding)Parameters
lineEndingString- the line ending
Throws
IllegalArgumentException- for anything but CR LF, LF or CR
isQuoteAll
public boolean isQuoteAll()Returns
setQuoteAll
public void setQuoteAll(boolean quoteAll)Parameters
quoteAllboolean- true to quote every value
isEscapeFormulas
public boolean isEscapeFormulas()Returns
setEscapeFormulas
public void setEscapeFormulas(boolean escapeFormulas)=, +, - 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
escapeFormulasboolean- true to prefix such values
isWriteByteOrderMark
public boolean isWriteByteOrderMark()write(OutputStream, String[][]) starts the file with a byte order mark.Returns
setWriteByteOrderMark
public void setWriteByteOrderMark(boolean writeByteOrderMark)write(OutputStream, String[][]) with the UTF-8 byte order
mark. Spreadsheets need it to read anything outside ASCII correctly.Parameters
writeByteOrderMarkboolean- true to write the mark
isWriteSeparatorHint
public boolean isWriteSeparatorHint()sep= line is written before the rows.Returns
setWriteSeparatorHint
public void setWriteSeparatorHint(boolean writeSeparatorHint)sep=, that tells a spreadsheet which separator the
file uses, whatever the locale of the machine. CSVParser reads that line when
separator detection is on.Parameters
writeSeparatorHintboolean- true to write the line
write
public void write(OutputStream out, String[][] rows)
throws IOExceptionParameters
outOutputStream- the destination
rowsString[][]- 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
write
public void write(Writer out, String[][] rows)
throws IOExceptionParameters
outWriter- the destination
rowsString[][]- the rows, each an array of values; a null value is written as an empty one
Throws
java.io.IOException- if the writer fails
writeHeader
public void writeHeader(Writer out)
throws IOExceptionsep= line if setWriteSeparatorHint(boolean) asked for one. Call it once,
before the first writeRow(Writer, String...), when writing row by row;
write(Writer, String[][]) calls it itself.Parameters
outWriter- the destination
Throws
java.io.IOException- if the writer fails
writeRow
public void writeRow(Writer out, String... cells)
throws IOExceptionParameters
outWriter- the destination
cellsString...- the values of the row; a null value is written as an empty one
Throws
java.io.IOException- if the writer fails
toString
public String toString(String[][] rows)Parameters
rowsString[][]- the rows, each an array of values