org.apache.commons.csv
Class CSVFormat

java.lang.Object
  extended by org.apache.commons.csv.CSVFormat
All Implemented Interfaces:
Serializable

public class CSVFormat
extends Object
implements Serializable

Specifies the format of a CSV file and parses input.

This class is immutable.

You can extend a format through a builder. For example, to extend the Excel format with columns header, you write:

CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").build();

You can parse through a format. For example, to parse an Excel file with columns header, you write:

Reader in = ...;
CSVFormat.EXCEL.toBuilder().withHeader("Col1", "Col2", "Col3").parse(in);

Version:
$Id: CSVFormat.java 1461240 2013-03-26 17:48:22Z ggregory $
See Also:
Serialized Form

Nested Class Summary
static class CSVFormat.CSVFormatBuilder
          Builds CSVFormat objects.
 
Field Summary
static CSVFormat DEFAULT
          Standard comma separated format, as for RFC4180 but allowing empty lines.
static CSVFormat EXCEL
          Excel file format (using a comma as the value delimiter).
static CSVFormat MYSQL
          Default MySQL format used by the SELECT INTO OUTFILE and LOAD DATA INFILE operations.
static CSVFormat RFC4180
          Comma separated format as defined by RFC 4180.
static CSVFormat TDF
          Tab-delimited format, with quote; leading and trailing spaces ignored.
 
Method Summary
 boolean equals(Object obj)
           
 String format(Object... values)
          Formats the specified values.
 Character getCommentStart()
          Returns the character marking the start of a line comment.
 char getDelimiter()
          Returns the character delimiting the values (typically ';', ',' or '\t').
 Character getEscape()
          Returns the escape character.
 boolean getIgnoreEmptyLines()
          Specifies whether empty lines between records are ignored when parsing input.
 boolean getIgnoreSurroundingSpaces()
          Specifies whether spaces around values are ignored when parsing input.
 Character getQuoteChar()
          Returns the character used to encapsulate values containing special characters.
 Quote getQuotePolicy()
          Returns the quote policy output fields.
 String getRecordSeparator()
          Returns the line separator delimiting output records.
 int hashCode()
           
 boolean isCommentingEnabled()
          Specifies whether comments are supported by this format.
 boolean isEscaping()
          Returns whether escape are being processed.
 boolean isQuoting()
          Returns whether an quoteChar has been defined.
static CSVFormat.CSVFormatBuilder newBuilder()
          Creates a standard comma separated format builder, as for RFC4180 but allowing empty lines.
static CSVFormat.CSVFormatBuilder newBuilder(char delimiter)
          Creates a new CSV format builder.
static CSVFormat.CSVFormatBuilder newBuilder(CSVFormat format)
          Creates a CSVFormatBuilder, using the values of the given CSVFormat.
 Iterable<CSVRecord> parse(Reader in)
          Parses the specified content.
 CSVFormat.CSVFormatBuilder toBuilder()
          Creates a builder based on this format.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

RFC4180

public static final CSVFormat RFC4180
Comma separated format as defined by RFC 4180.

RFC 4180:


DEFAULT

public static final CSVFormat DEFAULT
Standard comma separated format, as for RFC4180 but allowing empty lines.

RFC 4180:

Additional:


EXCEL

public static final CSVFormat EXCEL
Excel file format (using a comma as the value delimiter). Note that the actual value delimiter used by Excel is locale dependent, it might be necessary to customize this format to accommodate to your regional settings.

For example for parsing or generating a CSV file on a French system the following format will be used:

 CSVFormat fmt = CSVFormat.newBuilder(EXCEL).withDelimiter(';').build();
 
Settings are: Note: this is currently the same as RFC4180


TDF

public static final CSVFormat TDF
Tab-delimited format, with quote; leading and trailing spaces ignored.


MYSQL

public static final CSVFormat MYSQL
Default MySQL format used by the SELECT INTO OUTFILE and LOAD DATA INFILE operations. This is a tab-delimited format with a LF character as the line separator. Values are not quoted and special characters are escaped with '\'.

See Also:
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Method Detail

newBuilder

public static CSVFormat.CSVFormatBuilder newBuilder(char delimiter)
Creates a new CSV format builder.

Parameters:
delimiter - the char used for value separation, must not be a line break character
Returns:
a new CSV format builder.
Throws:
IllegalArgumentException - if the delimiter is a line break character

newBuilder

public static CSVFormat.CSVFormatBuilder newBuilder(CSVFormat format)
Creates a CSVFormatBuilder, using the values of the given CSVFormat.

Parameters:
format - The format to use values from
Returns:
a new CSVFormatBuilder

newBuilder

public static CSVFormat.CSVFormatBuilder newBuilder()
Creates a standard comma separated format builder, as for RFC4180 but allowing empty lines. Shortcut for CSVFormat.newBuilder(CSVFormat.DEFAULT)

Returns:
a standard comma separated format builder, as for RFC4180 but allowing empty lines.

getDelimiter

public char getDelimiter()
Returns the character delimiting the values (typically ';', ',' or '\t').

Returns:
the delimiter character

getQuoteChar

public Character getQuoteChar()
Returns the character used to encapsulate values containing special characters.

Returns:
the quoteChar character

isQuoting

public boolean isQuoting()
Returns whether an quoteChar has been defined.

Returns:
true if an quoteChar is defined

getCommentStart

public Character getCommentStart()
Returns the character marking the start of a line comment.

Returns:
the comment start marker.

isCommentingEnabled

public boolean isCommentingEnabled()
Specifies whether comments are supported by this format. Note that the comment introducer character is only recognised at the start of a line.

Returns:
true is comments are supported, false otherwise

getEscape

public Character getEscape()
Returns the escape character.

Returns:
the escape character

isEscaping

public boolean isEscaping()
Returns whether escape are being processed.

Returns:
true if escapes are processed

getIgnoreSurroundingSpaces

public boolean getIgnoreSurroundingSpaces()
Specifies whether spaces around values are ignored when parsing input.

Returns:
true if spaces around values are ignored, false if they are treated as part of the value.

getIgnoreEmptyLines

public boolean getIgnoreEmptyLines()
Specifies whether empty lines between records are ignored when parsing input.

Returns:
true if empty lines between records are ignored, false if they are turned into empty records.

getRecordSeparator

public String getRecordSeparator()
Returns the line separator delimiting output records.

Returns:
the line separator

parse

public Iterable<CSVRecord> parse(Reader in)
                          throws IOException
Parses the specified content.

Parameters:
in - the input stream
Returns:
a stream of CSVRecord
Throws:
IOException - If an I/O error occurs

format

public String format(Object... values)
Formats the specified values.

Parameters:
values - the values to format
Returns:
the formatted values

toString

public String toString()
Overrides:
toString in class Object

getQuotePolicy

public Quote getQuotePolicy()
Returns the quote policy output fields.

Returns:
the quote policy

hashCode

public int hashCode()
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

toBuilder

public CSVFormat.CSVFormatBuilder toBuilder()
Creates a builder based on this format.

Returns:
a new builder


Copyright © 2013 The Apache Software Foundation. All Rights Reserved.