Csv

A facility to read from and write to CSV (comma separated values) files.

Methods
char getEscapeCharacter()
Get the current escape character. 0 means no escape character is used.
char getFieldDelimiter()
Get the current field delimiter. 0 means no field delimiter is used.
char getFieldSeparatorRead()
Get the current field separator for reading.
String getFieldSeparatorWrite()
Get the current field separator for writing.
static Csv getInstance()
Get a new object of this class.
String getRowSeparatorWrite()
Get the current row separator for writing.
ResultSet read(String inputFileName, String[] colNames, String charset)
Reads from the CSV file and returns a result set.
ResultSet read(Reader reader, String[] colNames)
Reads CSV data from a reader and returns a result set.
void setEscapeCharacter(char escapeCharacter)
Set the escape character (used to escape the field delimiter).
void setFieldDelimiter(char fieldDelimiter)
Set the field delimiter.
void setFieldSeparatorRead(char fieldSeparatorRead)
Override the field separator for reading.
void setFieldSeparatorWrite(String fieldSeparatorWrite)
Override the field separator for writing.
void setLineSeparator(String lineSeparator)
Set the line separator.
void setNullString(String nullString)
Set the value that represents NULL.
void setRowSeparatorWrite(String rowSeparatorWrite)
Override the end-of-row marker for writing.
int write(Writer writer, ResultSet rs)
Writes the result set to a file in the CSV format.
int write(String outputFileName, ResultSet rs, String charset)
Writes the result set to a file in the CSV format.
int write(Connection conn, String outputFileName, String sql, String charset)
Writes the result set of a query to a file in the CSV format.

char getEscapeCharacter()

Get the current escape character. 0 means no escape character is used.

Returns:
the escape character

char getFieldDelimiter()

Get the current field delimiter. 0 means no field delimiter is used.

Returns:
the field delimiter

char getFieldSeparatorRead()

Get the current field separator for reading.

Returns:
the field separator

String getFieldSeparatorWrite()

Get the current field separator for writing.

Returns:
the field separator

static Csv getInstance()

Get a new object of this class.

Returns:
the new instance

String getRowSeparatorWrite()

Get the current row separator for writing.

Returns:
the row separator

ResultSet read(String inputFileName, String[] colNames, String charset) throws SQLException

Reads from the CSV file and returns a result set. The rows in the result set are created on demand, that means the file is kept open until all rows are read or the result set is closed.

Parameters:
inputFileName - the file name
colNames - or null if the column names should be read from the CSV file
charset - the charset or null to use UTF-8
Returns:
the result set
Throws:
SQLException

ResultSet read(Reader reader, String[] colNames) throws SQLException, IOException

Reads CSV data from a reader and returns a result set. The rows in the result set are created on demand, that means the reader is kept open until all rows are read or the result set is closed.

Parameters:
reader - the reader
colNames - or null if the column names should be read from the CSV file
Returns:
the result set
Throws:
SQLException, - IOException

void setEscapeCharacter(char escapeCharacter)

Set the escape character (used to escape the field delimiter). The default is " (a double quote). 0 means no escape character is used.

Parameters:
escapeCharacter - the escape character

void setFieldDelimiter(char fieldDelimiter)

Set the field delimiter. The default is " (a double quote). 0 means no field delimiter is used.

Parameters:
fieldDelimiter - the field delimiter

void setFieldSeparatorRead(char fieldSeparatorRead)

Override the field separator for reading. The default is ','.

Parameters:
fieldSeparatorRead - the field separator

void setFieldSeparatorWrite(String fieldSeparatorWrite)

Override the field separator for writing. The default is ",".

Parameters:
fieldSeparatorWrite - the field separator

void setLineSeparator(String lineSeparator)

Set the line separator.

Parameters:
lineSeparator - the line separator

void setNullString(String nullString)

Set the value that represents NULL.

Parameters:
nullString - the null

void setRowSeparatorWrite(String rowSeparatorWrite)

Override the end-of-row marker for writing. The default is null. After writing the end-of-row marker, a line feed is written (\n or \r\n depending on the system settings).

Parameters:
rowSeparatorWrite - the row separator

int write(Writer writer, ResultSet rs) throws SQLException

Writes the result set to a file in the CSV format.

Parameters:
writer - the writer
rs - the result set
Returns:
the number of rows written
Throws:
SQLException

int write(String outputFileName, ResultSet rs, String charset) throws SQLException

Writes the result set to a file in the CSV format.

Parameters:
outputFileName - the name of the csv file
rs - the result set
charset - the charset or null to use UTF-8
Returns:
the number of rows written
Throws:
SQLException

int write(Connection conn, String outputFileName, String sql, String charset) throws SQLException

Writes the result set of a query to a file in the CSV format.

Parameters:
conn - the connection
outputFileName - the file name
sql - the query
charset - the charset or null to use UTF-8
Returns:
the number of rows written
Throws:
SQLException