Top Description Fields Constructors Methods
org.h2.tools

public Class Csv

extends Object
implements SimpleRowSource
Class Inheritance
All Implemented Interfaces
org.h2.tools.SimpleRowSource
Imports
java.io.BufferedOutputStream, .BufferedReader, .BufferedWriter, .IOException, .OutputStream, .OutputStreamWriter, .Reader, .Writer, java.nio.charset.Charset, .StandardCharsets, java.sql.Connection, .ResultSet, .ResultSetMetaData, .SQLException, .Statement, .Types, java.util.ArrayList, org.h2.api.ErrorCode, org.h2.engine.Constants, org.h2.message.DbException, org.h2.mvstore.DataUtils, org.h2.store.fs.FileUtils, org.h2.util.IOUtils, .JdbcUtils, .StringUtils, .Utils

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.
Author
Thomas Mueller, Sylvain Cuaz

Field Summary

Modifier and TypeField and Description
private boolean
private String
private String[]
private boolean
private boolean
private char
private char
private char
private String
private String
private BufferedReader
private char[]
private int
private int
private int
private char
private String
private String
private Writer
private boolean
private boolean
private boolean

Constructor Summary

AccessConstructor and Description
public
Csv()

Method Summary

Modifier and TypeMethod and Description
public void
private static SQLException
private String
escape(String data)

public boolean

Returns:

whether column names are case sensitive
getCaseSensitiveColumnNames
()

Get the current case sensitive column names setting.

public char

Returns:

the escape character
getEscapeCharacter
()

Get the current escape character.

public char

Returns:

the field delimiter
getFieldDelimiter
()

Get the current field delimiter.

public char

Returns:

the field separator
getFieldSeparatorRead
()

Get the current field separator for reading.

public String

Returns:

the field separator
getFieldSeparatorWrite
()

Get the current field separator for writing.

public char

Returns:

the line comment character, or 0 if disabled
getLineCommentCharacter
()

Get the line comment character.

public String

Returns:

the line separator
getLineSeparator
()

Get the line separator used for writing.

public String

Returns:

the null string.
getNullString
()

Get the current null string.

public boolean

Returns:

the current value for the setting
getPreserveWhitespace
()

Whether whitespace in unquoted text is preserved.

public boolean

Returns:

the current value for the setting
getWriteColumnHeader
()

Whether the column header is written.

private void
init(String newFileName, String charset)

private void
private void
private static boolean
isParam(String key, String... values)

public boolean

Returns:

True if the null values are quoted.
isQuotedNulls
()

Returns true if the null values are quoted.

private static boolean
private void
private void
public ResultSet

Returns:

the result set
read
(String
the file name
inputFileName
,
String[]
or null if the column names should be read from the CSV file
colNames
,
String
the charset or null to use the system default charset
charset
)

Reads from the CSV file and returns a result set.

public ResultSet

Returns:

the result set
read
(Reader
the reader
reader
,
String[]
or null if the column names should be read from the CSV file
colNames
)

Reads CSV data from a reader and returns a result set.

private int
private int
private void
private String
private ResultSet
readResultSet(String[] colNames)

public Object[]
private String
public void
public void
setCaseSensitiveColumnNames(boolean
whether column names are case sensitive
caseSensitiveColumnNames
)

Override the case sensitive column names setting.

public void
setEscapeCharacter(char
the escape character
escapeCharacter
)

Set the escape character.

public void
setFieldDelimiter(char
the field delimiter
fieldDelimiter
)

Set the field delimiter.

public void
setFieldSeparatorRead(char
the field separator
fieldSeparatorRead
)

Override the field separator for reading.

public void
setFieldSeparatorWrite(String
the field separator
fieldSeparatorWrite
)

Override the field separator for writing.

public void
setLineCommentCharacter(char
the line comment character
lineCommentCharacter
)

Set the line comment character.

public void
setLineSeparator(String
the line separator
lineSeparator
)

Set the line separator used for writing.

public void
setNullString(String
the null
nullString
)

Set the value that represents NULL.

public String

Returns:

the character set
setOptions
(String
the options
options
)

INTERNAL.

public void
setPreserveWhitespace(boolean
the new value for the setting
value
)

Enable or disable preserving whitespace in unquoted text.

public void
setQuotedNulls(boolean
True if the null values must be quoted.
quotedNulls
)

Defines if the null values must be quoted.

public void
setWriteColumnHeader(boolean
the new value for the setting
value
)

Enable or disable writing the column header.

private String
public int

Returns:

the number of rows written
write
(Writer
the writer
writer
,
ResultSet
the result set
rs
)

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

public int

Returns:

the number of rows written
write
(String
the name of the csv file
outputFileName
,
ResultSet
the result set - the result set must be positioned before the first row.
rs
,
String
the charset or null to use the system default charset
charset
)

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

public int

Returns:

the number of rows written
write
(Connection
the connection
conn
,
String
the file name
outputFileName
,
String
the query
sql
,
String
the charset or null to use the system default charset (see system property file.encoding)
charset
)

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

private int
private void
writeRow(String[] values)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

caseSensitiveColumnNamesback to summary
private boolean caseSensitiveColumnNames
characterSetback to summary
private String characterSet
columnNamesback to summary
private String[] columnNames
endOfFileback to summary
private boolean endOfFile
endOfLineback to summary
private boolean endOfLine
escapeCharacterback to summary
private char escapeCharacter
fieldDelimiterback to summary
private char fieldDelimiter
fieldSeparatorReadback to summary
private char fieldSeparatorRead
fieldSeparatorWriteback to summary
private String fieldSeparatorWrite
fileNameback to summary
private String fileName
inputback to summary
private BufferedReader input
inputBufferback to summary
private char[] inputBuffer
inputBufferEndback to summary
private int inputBufferEnd
inputBufferPosback to summary
private int inputBufferPos
inputBufferStartback to summary
private int inputBufferStart
lineCommentback to summary
private char lineComment
lineSeparatorback to summary
private String lineSeparator
nullStringback to summary
private String nullString
outputback to summary
private Writer output
preserveWhitespaceback to summary
private boolean preserveWhitespace
quotedNullsback to summary
private boolean quotedNulls
writeColumnHeaderback to summary
private boolean writeColumnHeader

Constructor Detail

Csvback to summary
public Csv()

Method Detail

closeback to summary
public void close()

Implements org.h2.tools.SimpleRowSource.close.

INTERNAL

Annotations
@Override
convertExceptionback to summary
private static SQLException convertException(String message, Exception e)
escapeback to summary
private String escape(String data)
getCaseSensitiveColumnNamesback to summary
public boolean getCaseSensitiveColumnNames()

Get the current case sensitive column names setting.

Returns:boolean

whether column names are case sensitive

getEscapeCharacterback to summary
public char getEscapeCharacter()

Get the current escape character.

Returns:char

the escape character

getFieldDelimiterback to summary
public char getFieldDelimiter()

Get the current field delimiter.

Returns:char

the field delimiter

getFieldSeparatorReadback to summary
public char getFieldSeparatorRead()

Get the current field separator for reading.

Returns:char

the field separator

getFieldSeparatorWriteback to summary
public String getFieldSeparatorWrite()

Get the current field separator for writing.

Returns:String

the field separator

getLineCommentCharacterback to summary
public char getLineCommentCharacter()

Get the line comment character.

Returns:char

the line comment character, or 0 if disabled

getLineSeparatorback to summary
public String getLineSeparator()

Get the line separator used for writing.

Returns:String

the line separator

getNullStringback to summary
public String getNullString()

Get the current null string.

Returns:String

the null string.

getPreserveWhitespaceback to summary
public boolean getPreserveWhitespace()

Whether whitespace in unquoted text is preserved.

Returns:boolean

the current value for the setting

getWriteColumnHeaderback to summary
public boolean getWriteColumnHeader()

Whether the column header is written.

Returns:boolean

the current value for the setting

initback to summary
private void init(String newFileName, String charset)
initReadback to summary
private void initRead() throws IOException
initWriteback to summary
private void initWrite() throws IOException
isParamback to summary
private static boolean isParam(String key, String... values)
isQuotedNullsback to summary
public boolean isQuotedNulls()

Returns true if the null values are quoted.

Returns:boolean

True if the null values are quoted.

isSimpleColumnNameback to summary
private static boolean isSimpleColumnName(String columnName)
makeColumnNamesUniqueback to summary
private void makeColumnNamesUnique()
pushBackback to summary
private void pushBack()
readback to summary
public 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:String

the file name

colNames:String[]

or null if the column names should be read from the CSV file

charset:String

the charset or null to use the system default charset

Returns:ResultSet

the result set

Exceptions
SQLException:
on failure
readback to summary
public 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:Reader

the reader

colNames:String[]

or null if the column names should be read from the CSV file

Returns:ResultSet

the result set

Exceptions
IOException:
on failure
readBufferback to summary
private int readBuffer() throws IOException
readCharback to summary
private int readChar() throws IOException
readHeaderback to summary
private void readHeader() throws IOException
readNullback to summary
private String readNull(String s)
readResultSetback to summary
private ResultSet readResultSet(String[] colNames) throws IOException
readRowback to summary
public Object[] readRow() throws SQLException

Implements org.h2.tools.SimpleRowSource.readRow.

INTERNAL

Returns:Object[]

Doc from org.h2.tools.SimpleRowSource.readRow.

the row or null

Annotations
@Override
Exceptions
SQLException:

Doc from org.h2.tools.SimpleRowSource.readRow.

on failure

readValueback to summary
private String readValue() throws IOException
resetback to summary
public void reset() throws SQLException

Implements org.h2.tools.SimpleRowSource.reset.

INTERNAL

Annotations
@Override
Exceptions
SQLException:

Doc from org.h2.tools.SimpleRowSource.reset.

if this operation is not supported

setCaseSensitiveColumnNamesback to summary
public 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:boolean

whether column names are case sensitive

setEscapeCharacterback to summary
public 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:char

the escape character

setFieldDelimiterback to summary
public 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:char

the field delimiter

setFieldSeparatorReadback to summary
public void setFieldSeparatorRead(char fieldSeparatorRead)

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

Parameters
fieldSeparatorRead:char

the field separator

setFieldSeparatorWriteback to summary
public void setFieldSeparatorWrite(String fieldSeparatorWrite)

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

Parameters
fieldSeparatorWrite:String

the field separator

setLineCommentCharacterback to summary
public void setLineCommentCharacter(char lineCommentCharacter)

Set the line comment character. The default is character code 0 (line comments are disabled).

Parameters
lineCommentCharacter:char

the line comment character

setLineSeparatorback to summary
public void setLineSeparator(String lineSeparator)

Set the line separator used for writing. This is usually a line feed (\n or \r\n depending on the system settings). The line separator is written after each row (including the last row), so this option can include an end-of-row marker if needed.

Parameters
lineSeparator:String

the line separator

setNullStringback to summary
public void setNullString(String nullString)

Set the value that represents NULL. It is only used for non-delimited values.

Parameters
nullString:String

the null

setOptionsback to summary
public String setOptions(String options)

INTERNAL. Parse and set the CSV options.

Parameters
options:String

the options

Returns:String

the character set

setPreserveWhitespaceback to summary
public void setPreserveWhitespace(boolean value)

Enable or disable preserving whitespace in unquoted text.

Parameters
value:boolean

the new value for the setting

setQuotedNullsback to summary
public void setQuotedNulls(boolean quotedNulls)

Defines if the null values must be quoted.

Parameters
quotedNulls:boolean

True if the null values must be quoted.

setWriteColumnHeaderback to summary
public void setWriteColumnHeader(boolean value)

Enable or disable writing the column header.

Parameters
value:boolean

the new value for the setting

unEscapeback to summary
private String unEscape(String s)
writeback to summary
public int write(Writer writer, ResultSet rs) throws SQLException

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

Parameters
writer:Writer

the writer

rs:ResultSet

the result set

Returns:int

the number of rows written

Exceptions
SQLException:
on failure
writeback to summary
public 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:String

the name of the csv file

rs:ResultSet

the result set - the result set must be positioned before the first row.

charset:String

the charset or null to use the system default charset

Returns:int

the number of rows written

Exceptions
SQLException:
on failure
writeback to summary
public 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:Connection

the connection

outputFileName:String

the file name

sql:String

the query

charset:String

the charset or null to use the system default charset (see system property file.encoding)

Returns:int

the number of rows written

Exceptions
SQLException:
on failure
writeResultSetback to summary
private int writeResultSet(ResultSet rs) throws SQLException
writeRowback to summary
private void writeRow(String[] values) throws IOException