A facility to read from and write to CSV (comma separated values) files. When
reading, the BOM (the byte-order-mark) character 0xfeff at the beginning of
the file is ignored.
Methods |
boolean |
getCaseSensitiveColumnNames()
Get the current case sensitive column names setting.
|
boolean |
getCaseSensitiveColumnNames()
Get the current case sensitive column names setting.
Returns:
whether column names are case sensitive
|
char |
getEscapeCharacter()
Get the current escape character.
|
char |
getEscapeCharacter()
Get the current escape character.
Returns:
the escape character
|
char |
getFieldDelimiter()
Get the current field delimiter.
|
char |
getFieldDelimiter()
Get the current field delimiter.
Returns:
the field delimiter
|
char |
getFieldSeparatorRead()
Get the current field separator for reading.
|
char |
getFieldSeparatorRead()
Get the current field separator for reading.
Returns:
the field separator
|
String |
getFieldSeparatorWrite()
Get the current field separator for writing.
|
String |
getFieldSeparatorWrite()
Get the current field separator for writing.
Returns:
the field separator
|
char |
()
Get the line comment character.
|
char |
()
Get the line comment character.
Returns:
the line comment character, or 0 if disabled
|
String |
getLineSeparator()
Get the current line separator.
|
String |
getLineSeparator()
Get the current line separator.
Returns:
the line separator
|
String |
getNullString()
Get the current null string.
|
String |
getNullString()
Get the current null string.
Returns:
the null string.
|
boolean |
getPreserveWhitespace()
Whether whitespace in unquoted text is preserved.
|
boolean |
getPreserveWhitespace()
Whether whitespace in unquoted text is preserved.
Returns:
the current value for the setting
|
String |
getRowSeparatorWrite()
Get the current row separator for writing.
|
String |
getRowSeparatorWrite()
Get the current row separator for writing.
Returns:
the row separator
|
boolean |
getWriteColumnHeader()
Whether the column header is written.
|
boolean |
getWriteColumnHeader()
Whether the column header is written.
Returns:
the current value for the setting
|
ResultSet |
read(String inputFileName, String[] colNames, String charset)
Reads from the CSV file and returns a result set.
|
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.
If the columns are read from the CSV file, then the following rules are
used: columns names that start with a letter or '_', and only
contain letters, '_', and digits, are considered case insensitive
and are converted to uppercase. Other column names are considered
case sensitive (that means they need to be quoted when accessed).
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 the system default charset
(see system property file.encoding)
Returns:
the result set
|
ResultSet |
read(Reader reader, String[] colNames)
Reads CSV data from a reader and returns a result set.
|
ResultSet |
read(Reader reader, String[] colNames) throws 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
|
void |
setCaseSensitiveColumnNames(boolean caseSensitiveColumnNames)
Override the case sensitive column names setting.
|
void |
setCaseSensitiveColumnNames(boolean caseSensitiveColumnNames)
Override the case sensitive column names setting. The default is false.
If enabled, the case of all column names is always preserved.
Parameters:
caseSensitiveColumnNames - whether column names are case sensitive
|
void |
setEscapeCharacter(char escapeCharacter)
Set the escape character.
|
void |
setEscapeCharacter(char escapeCharacter)
Set the escape character. The escape character is used to escape the
field delimiter. This is needed if the data contains the field delimiter.
The default escape character is " (a double quote), which is the same as
the field delimiter. If the field delimiter and the escape character are
both " (double quote), and the data contains a double quote, then an
additional double quote is added. Example:
Data: He said "Hello".
Escape character: "
Field delimiter: "
CSV file: "He said ""Hello""."
If the field delimiter is a double quote and the escape character is a
backslash, then escaping is done similar to Java (however, only the field
delimiter is escaped). Example:
Data: He said "Hello".
Escape character: \
Field delimiter: "
CSV file: "He said \"Hello\"."
The value 0 means no escape character is used.
Parameters:
escapeCharacter - the escape character
|
void |
setFieldDelimiter(char fieldDelimiter)
Set the field delimiter.
|
void |
setFieldDelimiter(char fieldDelimiter)
Set the field delimiter. The default is " (a double quote).
The value 0 means no field delimiter is used.
Parameters:
fieldDelimiter - the field delimiter
|
void |
setFieldSeparatorRead(char fieldSeparatorRead)
Override the field separator for reading.
|
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.
|
void |
setFieldSeparatorWrite(String fieldSeparatorWrite)
Override the field separator for writing. The default is ",".
Parameters:
fieldSeparatorWrite - the field separator
|
void |
(char lineCommentCharacter)
Set the line comment character.
|
void |
(char lineCommentCharacter)
Set the line comment character. The default is character code 0 (line
comments are disabled).
Parameters:
lineCommentCharacter - the line comment character
|
void |
setLineSeparator(String lineSeparator)
Set the line separator.
|
void |
setLineSeparator(String lineSeparator)
Set the line separator.
Parameters:
lineSeparator - the line separator
|
void |
setNullString(String nullString)
Set the value that represents NULL.
|
void |
setNullString(String nullString)
Set the value that represents NULL. It is only used for non-delimited
values.
Parameters:
nullString - the null
|
void |
setPreserveWhitespace(boolean value)
Enable or disable preserving whitespace in unquoted text.
|
void |
setPreserveWhitespace(boolean value)
Enable or disable preserving whitespace in unquoted text.
Parameters:
value - the new value for the setting
|
void |
setRowSeparatorWrite(String rowSeparatorWrite)
Override the end-of-row marker for writing.
|
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
|
void |
setWriteColumnHeader(boolean value)
Enable or disable writing the column header.
|
void |
setWriteColumnHeader(boolean value)
Enable or disable writing the column header.
Parameters:
value - the new value for the setting
|
int |
write(Writer writer, ResultSet rs)
Writes the result set to a file in the CSV format.
|
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
|
int |
write(String outputFileName, ResultSet rs, String charset)
Writes the result set to a file in the CSV format.
|
int |
write(String outputFileName, ResultSet rs, String charset) throws SQLException
Writes the result set to a file in the CSV format. The result set is read
using the following loop:
while (rs.next()) {
writeRow(row);
}
Parameters:
outputFileName - the name of the csv file
rs - the result set - the result set must be positioned before the
first row.
charset - the charset or null to use the system default charset
(see system property file.encoding)
Returns:
the number of rows written
|
int |
write(Connection conn, String outputFileName, String sql, String charset)
Writes the result set of a query to a file in the CSV format.
|
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 the system default charset
(see system property file.encoding)
Returns:
the number of rows written
|