public final class CsvOutput extends Object
Provides a simple tool for writing a CSV file.
Each line in the CSV file will consist of comma separated entries. Each entry may be quoted using a double quote. If an entry contains a double quote, comma or trimmable whitespace, it will be quoted. If an entry starts with '=' or '@', it will be quoted. Two double quotes will be used to escape a double quote.
There are two modes of output. Standard mode provides the encoding described above which is accepted by most CSV parsers. Safe mode provides extra encoding to protect unsafe content from being run as a script in tools like Excel.
Instances of this class contain mutable state. A new instance must be created for each file to be output.
| Modifier and Type | Class and Description |
|---|---|
class |
CsvOutput.CsvRowOutputWithHeaders
Class used when outputting CSV with headers.
|
| Modifier and Type | Method and Description |
|---|---|
static CsvOutput |
safe(Appendable underlying)
Creates an instance, using the system default line separator and using a comma separator.
|
static CsvOutput |
safe(Appendable underlying,
String newLine)
Creates an instance, allowing the new line character to be controlled and using a comma separator.
|
static CsvOutput |
safe(Appendable underlying,
String newLine,
String separator)
Creates an instance, allowing the new line character to be controlled, specifying the separator.
|
static CsvOutput |
standard(Appendable underlying)
Creates an instance, using the system default line separator and using a comma separator.
|
static CsvOutput |
standard(Appendable underlying,
String newLine)
Creates an instance, allowing the new line character to be controlled and using a comma separator.
|
static CsvOutput |
standard(Appendable underlying,
String newLine,
String separator)
Creates an instance, allowing the new line character to be controlled, specifying the separator.
|
CsvOutput.CsvRowOutputWithHeaders |
withHeaders(List<String> headers,
boolean alwaysQuote)
Write a header line to the underlying, returning an instance that allows
cells to be written by header name.
|
CsvOutput |
writeCell(String cell)
Writes a single cell to the current line, only quoting if needed.
|
CsvOutput |
writeCell(String cell,
boolean alwaysQuote)
Writes a single cell to the current line.
|
void |
writeCsvFile(CsvFile file,
boolean alwaysQuote)
Writes the provided
CsvFile to the underlying. |
void |
writeCsvIterator(CsvIterator iterator,
boolean alwaysQuote)
Writes the output of the provided
CsvIterator to the underlying. |
void |
writeLine(List<String> line)
Writes a single CSV line to the underlying, only quoting if needed.
|
void |
writeLine(List<String> line,
boolean alwaysQuote)
Writes a single CSV line to the underlying.
|
void |
writeLines(Iterable<? extends List<String>> lines,
boolean alwaysQuote)
Writes multiple CSV lines to the underlying.
|
CsvOutput |
writeNewLine()
Writes a new line character.
|
void |
writeRow(CsvRow row)
Writes a single
CsvRow to the underlying, only quoting if needed. |
void |
writeRow(CsvRow row,
boolean alwaysQuote)
Writes a single
CsvRow to the underlying. |
void |
writeRows(Iterable<CsvRow> rows,
boolean alwaysQuote)
Writes multiple
CsvRows to the underlying. |
public static CsvOutput standard(Appendable underlying)
See the standard quoting rules in the class-level documentation.
underlying - the destination to write topublic static CsvOutput standard(Appendable underlying, String newLine)
See the standard quoting rules in the class-level documentation.
underlying - the destination to write tonewLine - the new line stringpublic static CsvOutput standard(Appendable underlying, String newLine, String separator)
See the standard quoting rules in the class-level documentation.
underlying - the destination to write tonewLine - the new line stringseparator - the separator used to separate each field, typically a comma, but a tab is sometimes usedpublic static CsvOutput safe(Appendable underlying)
This applies the standard quoting rules from the class-level documentation, plus an additional rule. If an entry starts with an expression character, '=', '@', '+' or '-', the entry will be quoted and the quote section will be preceeded by equals. Thus, the string '=Foo' will be written as '="=Foo"'. This avoids the string being treated as an expression by tools like Excel. Simple numbers are not quoted. Thus, the number '-1234' will still be written as '-1234'.
underlying - the destination to write topublic static CsvOutput safe(Appendable underlying, String newLine)
This applies the standard quoting rules from the class-level documentation, plus an additional rule. If an entry starts with an expression character, '=', '@', '+' or '-', the entry will be quoted and the quote section will be preceeded by equals. Thus, the string '=Foo' will be written as '="=Foo"'. This avoids the string being treated as an expression by tools like Excel. Simple numbers are not quoted. Thus, the number '-1234' will still be written as '-1234'.
underlying - the destination to write tonewLine - the new line stringpublic static CsvOutput safe(Appendable underlying, String newLine, String separator)
This applies the standard quoting rules from the class-level documentation, plus an additional rule. If an entry starts with an expression character, '=', '@', '+' or '-', the entry will be quoted and the quote section will be preceeded by equals. Thus, the string '=Foo' will be written as '="=Foo"'. This avoids the string being treated as an expression by tools like Excel. Simple numbers are not quoted. Thus, the number '-1234' will still be written as '-1234'.
underlying - the destination to write tonewLine - the new line stringseparator - the separator used to separate each field, typically a comma, but a tab is sometimes usedpublic CsvOutput.CsvRowOutputWithHeaders withHeaders(List<String> headers, boolean alwaysQuote)
This writes the header line to the underlying.
This is normally called once when the CsvOutput instance is created
with only the returned instance used to write additional rows.
While it is possible to write rows through both instances, this is likely to get confusing.
headers - the list of headersalwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeLines(Iterable<? extends List<String>> lines, boolean alwaysQuote)
The boolean flag controls whether each entry is always quoted or only quoted when necessary.
lines - the lines to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeLine(List<String> line)
This can be used as a method reference from a Stream pipeline from
Stream.forEachOrdered(Consumer).
This method writes each cell in the specified list to the underlying, followed by a new line character.
line - the line to writeUncheckedIOException - if an IO exception occurspublic void writeLine(List<String> line, boolean alwaysQuote)
The boolean flag controls whether each entry is always quoted or only quoted when necessary.
This method writes each cell in the specified list to the underlying, followed by a new line character.
line - the line to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeRows(Iterable<CsvRow> rows, boolean alwaysQuote)
CsvRows to the underlying.
The boolean flag controls whether each entry is always quoted or only quoted when necessary.
rows - the rows to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeRow(CsvRow row)
CsvRow to the underlying, only quoting if needed.
This can be used as a method reference from a Stream pipeline from
Stream.forEachOrdered(Consumer).
This method writes each field in the specified row to the underlying, followed by a new line character.
row - the row to writeUncheckedIOException - if an IO exception occurspublic void writeRow(CsvRow row, boolean alwaysQuote)
CsvRow to the underlying.
The boolean flag controls whether each entry is always quoted or only quoted when necessary.
This method writes each field in the specified row to the underlying, followed by a new line character.
row - the row to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeCsvFile(CsvFile file, boolean alwaysQuote)
CsvFile to the underlying.file - the file whose contents to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic void writeCsvIterator(CsvIterator iterator, boolean alwaysQuote)
CsvIterator to the underlying.iterator - the iterator whose output to writealwaysQuote - when true, each column will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic CsvOutput writeCell(String cell)
When using this method, either writeNewLine() or one of the writeLine
methods must be called at the end of the line.
cell - the cell to writeUncheckedIOException - if an IO exception occurspublic CsvOutput writeCell(String cell, boolean alwaysQuote)
The boolean flag controls whether each entry is always quoted or only quoted when necessary.
When using this method, either writeNewLine() or one of the writeLine
methods must be called at the end of the line.
cell - the cell to writealwaysQuote - when true, the cell will be quoted, when false, quoting is selectiveUncheckedIOException - if an IO exception occurspublic CsvOutput writeNewLine()
UncheckedIOException - if an IO exception occursCopyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.