Class CSVWriter

java.lang.Object
com.codename1.io.CSVWriter

public class CSVWriter extends Object

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
    Constructor
    Description
    Creates a writer with the comma separator.
    CSVWriter(char separatorChar)
    Creates a writer with a custom separator.
  • Method Summary

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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      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: 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. CSVParser reads that line when separator detection is on.

      Parameters
      • writeSeparatorHint: 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: 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

      public void write(Writer out, String[][] rows) throws IOException

      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

      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: the destination
      Throws
      • java.io.IOException: if the writer fails
      Throws:
      IOException
    • 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: 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

      public String toString(String[][] rows)

      Returns rows as CSV text.

      Parameters
      • rows: the rows, each an array of values
      Returns

      the text