Skip navigation links
com.opengamma.strata.collect.io

## Class CsvOutput

• public final class CsvOutput
extends Object
Outputs a CSV formatted file.

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.

• ### Constructor Summary

Constructors
Constructor and Description
CsvOutput(Appendable underlying)
Deprecated.
Use one of the static factory methods
CsvOutput(Appendable underlying, String newLine)
Deprecated.
Use one of the static factory methods
CsvOutput(Appendable underlying, String newLine, String separator)
Deprecated.
Use one of the static factory methods
• ### Method Summary

All Methods
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.
void writeLine(List<String> line)
Writes a CSV line to the underlying, only quoting if needed.
void writeLine(List<String> line, boolean alwaysQuote)
Writes a CSV line to the underlying.
void writeLines(Iterable<? extends List<String>> lines, boolean alwaysQuote)
Writes CSV lines to the underlying.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Constructor Detail

• #### CsvOutput

@Deprecated
public CsvOutput(Appendable underlying)
Deprecated. Use one of the static factory methods
Creates an instance, using the system default line separator and using a comma separator.
Parameters:
underlying - the destination to write to
• #### CsvOutput

@Deprecated
public CsvOutput(Appendable underlying,
String newLine)
Deprecated. Use one of the static factory methods
Creates an instance, allowing the new line character to be controlled and using a comma separator.
Parameters:
underlying - the destination to write to
newLine - the new line string
• #### CsvOutput

@Deprecated
public CsvOutput(Appendable underlying,
String newLine,
String separator)
Deprecated. Use one of the static factory methods
Creates an instance, allowing the new line character to be controlled, specifying the separator.
Parameters:
underlying - the destination to write to
newLine - the new line string
separator - the separator used to separate each field, typically a comma, but a tab is sometimes used
• ### Method Detail

• #### standard

public static CsvOutput standard(Appendable underlying)
Creates an instance, using the system default line separator and using a comma separator.

See the standard quoting rules in the class-level documentation.

Parameters:
underlying - the destination to write to
Returns:
the CSV outputter
• #### standard

public static CsvOutput standard(Appendable underlying,
String newLine)
Creates an instance, allowing the new line character to be controlled and using a comma separator.

See the standard quoting rules in the class-level documentation.

Parameters:
underlying - the destination to write to
newLine - the new line string
Returns:
the CSV outputter
• #### standard

public static CsvOutput standard(Appendable underlying,
String newLine,
String separator)
Creates an instance, allowing the new line character to be controlled, specifying the separator.

See the standard quoting rules in the class-level documentation.

Parameters:
underlying - the destination to write to
newLine - the new line string
separator - the separator used to separate each field, typically a comma, but a tab is sometimes used
Returns:
the CSV outputter
• #### safe

public static CsvOutput safe(Appendable underlying)
Creates an instance, using the system default line separator and using a comma 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'.

Parameters:
underlying - the destination to write to
Returns:
the CSV outputter
• #### safe

public static CsvOutput safe(Appendable underlying,
String newLine)
Creates an instance, allowing the new line character to be controlled and using a comma 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'.

Parameters:
underlying - the destination to write to
newLine - the new line string
Returns:
the CSV outputter
• #### safe

public static CsvOutput safe(Appendable underlying,
String newLine,
String separator)
Creates an instance, allowing the new line character to be controlled, specifying the 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'.

Parameters:
underlying - the destination to write to
newLine - the new line string
separator - the separator used to separate each field, typically a comma, but a tab is sometimes used
Returns:
the CSV outputter
• #### writeLines

public void writeLines(Iterable<? extends List<String>> lines,
boolean alwaysQuote)
Writes CSV lines to the underlying.

The boolean flag controls whether each entry is always quoted or only quoted when necessary.

Parameters:
lines - the lines to write
alwaysQuote - when true, each column will be quoted, when false, quoting is selective
Throws:
UncheckedIOException - if an IO exception occurs
• #### writeLine

public void writeLine(List<String> line)
Writes a CSV line to the underlying, only quoting if needed.

This can be used as a method reference from a Stream pipeline from Stream.forEachOrdered(Consumer).

Parameters:
line - the line to write
Throws:
UncheckedIOException - if an IO exception occurs
• #### writeLine

public void writeLine(List<String> line,
boolean alwaysQuote)
Writes a CSV line to the underlying.

The boolean flag controls whether each entry is always quoted or only quoted when necessary.

Parameters:
line - the line to write
alwaysQuote - when true, each column will be quoted, when false, quoting is selective
Throws:
UncheckedIOException - if an IO exception occurs
Skip navigation links

Copyright 2009-Present by OpenGamma Inc. and individual contributors
Apache v2 licensed
Additional documentation can be found at strata.opengamma.io.