# Class CsvTableWriter

java.lang.Object
uk.ac.starlink.table.formats.DocumentedStreamStarTableWriter
uk.ac.starlink.table.formats.CsvTableWriter

All Implemented Interfaces:
  `Documented, DocumentedIOHandler, StarTableWriter`

public class CsvTableWriter extends DocumentedStreamStarTableWriter
A StarTableWriter which outputs to Comma-Separated Value format. This format is readable by `CsvTableBuilder`.

Since:
  21 Sep 2004
Author:
  Mark Taylor (Starlink)

## Constructor Summary

Constructors
Constructor
Description
`CsvTableWriter()`
Constructs a default CSV table writer.
`CsvTableWriter(boolean writeHeader)`
Constructs a CSV table writer which optionally writes headers.

## Method Summary

Modifier and Type
Method
Description
`boolean`
`docIncludesExample()`
Indicates whether the serialization of some (short) example table should be added to the user documentation for this handler.
`char`
`getDelimiter()`
Returns the delimiter character.
`String`
`getFormatName()`
Returns "CSV" or "CSV-noheader".
`int`
`getMaxFieldChars()`
Returns the limit on the number of characters that will be written in a single field.
`String`
`getMimeType()`
Returns a string suitable for use as the value of a MIME Content-Type header.
`boolean`
`getWriteHeader()`
Indicates whether an initial row containing column names will be written.
`String`
`getXmlDescription()`
Returns user-directed documentation in XML format.
`void`
`setDelimiter(char delimiter)`
Sets the delimiter character.
`void`
`setMaxFieldChars(int maxFieldChars)`
Sets a limit on the number of characters that will be written in a single field.
`void`
`setWriteHeader(boolean writeHeader)`
Indicate whether an initial row containing column names should be written.
`void`
`writeStarTable(StarTable table, OutputStream ostrm)`
Writes a `StarTable` object to a given output stream.

### Methods inherited from class DocumentedStreamStarTableWriter

`getExtensions, looksLikeFile, writeStarTable`

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Methods inherited from interface DocumentedIOHandler

`readText`

## Constructor Details

### CsvTableWriter

public CsvTableWriter()
Constructs a default CSV table writer. This doesn't write a header.

### CsvTableWriter

public CsvTableWriter(boolean writeHeader)
Constructs a CSV table writer which optionally writes headers.

Parameters:
  `writeHeader` - true iff you want the first output line to contain column names

## Method Details

### setWriteHeader

@ConfigMethod(property="header", doc="<p>If true, the first line of the CSV output will be a header containing the column names; if false, no header line is written and all lines represent data rows.</p>", sequence=1) public void setWriteHeader(boolean writeHeader)
Indicate whether an initial row containing column names should be written.

Parameters:
  `writeHeader` - true iff you want the first output line to contain column names

### getWriteHeader

public boolean getWriteHeader()
Indicates whether an initial row containing column names will be written.

Returns:
  whether the first output line will contain column names

### setDelimiter

@ConfigMethod(property="delimiter", doc="<p>Field delimiter character, by default a comma. Permitted values are a single character like \"<code>|</code>\", a hexadecimal character code like \"<code>0x7C</code>\", or one of the names \"<code>comma</code>\", \"<code>space</code>\" or \"<code>tab</code>\". Some choices of delimiter, for instance whitespace characters, might not work well or might behave in surprising ways.</p>", example="|", sequence=2) public void setDelimiter(char delimiter)
Sets the delimiter character. Non-comma delimiters are not guaranteed to work.

Parameters:
  `delimiter` - delimiter character

### getDelimiter

public char getDelimiter()
Returns the delimiter character.

Returns:
  delimiter

### setMaxFieldChars

@ConfigMethod(property="maxCell", doc="<p>Maximum width in characters of an output table cell. Cells longer than this will be truncated.</p>", example="160", sequence=3) public void setMaxFieldChars(int maxFieldChars)
Sets a limit on the number of characters that will be written in a single field. Fields beyond this length will be truncated.

Parameters:
  `maxFieldChars` - new limit

### getMaxFieldChars

public int getMaxFieldChars()
Returns the limit on the number of characters that will be written in a single field. Fields beyond this length will be truncated.

Returns:
  current limit

### getFormatName

public String getFormatName()
Returns "CSV" or "CSV-noheader".

Returns:
  a short string identifying the output format of this writer

### getMimeType

public String getMimeType()
Description copied from interface: `StarTableWriter`
Returns a string suitable for use as the value of a MIME Content-Type header. If no suitable MIME type is available or known, one of "`application/octet-stream`" (for binary formats) or "`text/plain`" for ASCII ones) is recommended.

Returns:
  MIME content type

### docIncludesExample

public boolean docIncludesExample()
Description copied from interface: `DocumentedIOHandler`
Indicates whether the serialization of some (short) example table should be added to the user documentation for this handler. Binary formats, or instances for which the `Documented.getXmlDescription()` method already includes some example output, should return false.

Returns:
  true if the user documentation would benefit from the addition of an example serialization

### getXmlDescription

public String getXmlDescription()
Description copied from interface: `Documented`
Returns user-directed documentation in XML format.

The output should be a sequence of one or more <P> elements, using XHTML-like XML. Since rendering may be done in a number of contexts however, use of the full range of XHTML elements is discouraged. Where possible, the content should stick to simple markup such as the elements P, A, UL, OL, LI, DL, DT, DD EM, STRONG, I, B, CODE, TT, PRE.

Returns:
  XML description of this object

### writeStarTable

public void writeStarTable(StarTable table, OutputStream ostrm) throws IOException
Description copied from interface: `StarTableWriter`
Writes a `StarTable` object to a given output stream. The implementation can assume that `out` is suitable for direct writing (for instance it should not normally wrap it in a `BufferedOutputStream`), and should not close it at the end of the call.

Not all table writers are capable of writing to a stream; an implementation may throw a `TableFormatException` to indicate that it cannot do so.

Parameters:
  `table` - the table to write
  `ostrm` - the output stream to which `startab` should be written
Throws:
  `IOException` - if there is some I/O error