Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public class CSVWriter

  1. Object
  2. 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

public char getSeparatorChar()The separator written between the values of a row.
public char getQuoteChar()The character that encloses a quoted value.
public void setQuoteChar(char quoteChar)Changes the character that encloses a quoted value.
public String getLineEnding()The text written after every row.
public void setLineEnding(String lineEnding)Changes the text written after every row.
public boolean isQuoteAll()Whether every value is quoted.
public void setQuoteAll(boolean quoteAll)Quotes every value, including those that do not need it.
public boolean isEscapeFormulas()Whether values a spreadsheet would run as a formula are neutralized.
public void setEscapeFormulas(boolean escapeFormulas)Neutralizes values a spreadsheet would run as a formula.
public boolean isWriteByteOrderMark()Whether write(OutputStream, String[][]) starts the file with a byte order mark.
public void setWriteByteOrderMark(boolean writeByteOrderMark)Starts a file written by write(OutputStream, String[][]) with the UTF-8 byte order mark.
public boolean isWriteSeparatorHint()Whether a sep= line is written before the rows.
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.
public void write(OutputStream out, String[][] rows) throws IOExceptionWrites rows as UTF-8.
public void write(Writer out, String[][] rows) throws IOExceptionWrites rows.
public void writeHeader(Writer out) throws IOExceptionWrites the sep= line if setWriteSeparatorHint(boolean) asked for one.
public void writeRow(Writer out, String... cells) throws IOExceptionWrites one row followed by the line ending, for output that is produced row by row.
public String toString(String[][] rows)Returns rows as CSV text.

Inherited methods

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 char
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 char
the quote character, a double quote by default

Throws

IllegalArgumentException
for the separator or a line break

getLineEnding

public String getLineEnding()
The text written after every row.

Returns

the line ending

setLineEnding

public void setLineEnding(String lineEnding)
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 String
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 boolean
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 boolean
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 boolean
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. CSVParser reads that line when separator detection is on.

Parameters

writeSeparatorHint boolean
true to write the line

write

public void write(OutputStream out, String[][] rows) throws IOException
Writes rows as UTF-8. The stream is flushed and left open.

Parameters

out OutputStream
the destination
rows String[][]
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 IOException
Writes rows. The writer is left open.

Parameters

out Writer
the destination
rows String[][]
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 IOException
Writes the sep= 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

out Writer
the destination

Throws

java.io.IOException
if the writer fails

writeRow

public void writeRow(Writer out, String... cells) throws IOException
Writes one row followed by the line ending, for output that is produced row by row.

Parameters

out Writer
the destination
cells String...
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)
Returns rows as CSV text.

Parameters

rows String[][]
the rows, each an array of values

Returns

the text