Examples

Reading and writing CSV files using easyCSV is very easy. There are several data handlers for differed types of input and output mechanisms.

These are:

  • CsvFileHandler (reads and writes data from and to files)
  • CsvStreamHandler (reads and writes data from and to Input- / OutputStreams)
  • CsvStringHandler (reads and writes data from plain Strings)
  • CsvByteArrayHandler (reads and writes data from and to byte arrays)
  • ExcelHandler (reads and writes data from Excel files)
  • ResultSetHandler (reads data from JDBC ResultSets)
  • HtmlHandler (exports CSV data to HTML)

Reading CSV

Lets show a few simple examples of reading CSV data. Most handlers have more methods for reading and writing data - see the Javadocs for a complete reference.

Reading CSV from a file

File file = new File("/tmp/myfile.csv");
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = CsvFileHandler.importCsv(importSettings, file);

Reading CSV from a stream

InputStream is = ...
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = CsvStreamHandler.importCsv(importSettings, is);

Reading CSV from a simple string

String string = "column A, column B, columnC\n" +
                "value A, value B, value C\n";
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = CsvStringHandler.importCsv(importSettings, string);

Reading CSV from a byte array

byte[] data = ...
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = CsvByteArrayHandler.importCsv(importSettings, data);

Reading CSV from a excel file

File file = new File("/tmp/myfile.xls");
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = ExcelHandler.importCsv(importSettings, file);

Reading CSV from a JDBC ResultSet

PreparedStatement preparedStatement = ...
ResultSet resultSet = preparedStatement.executeQuery();

ImportSettings importSettings = new ImportSettings();
CsvDataStore store = ResultSetHandler.importCsv(importSettings, resultSet);

Writing CSV

Lets show a few simple examples of writing CSV data. Most handlers have more methods for reading and writing data - see the Javadocs for a complete reference.

Writing CSV to a file

CsvDataStore store = ...
File file = new File("/tmp/myfile.csv");

ExportSettings exportSettings = new ExportSettings();
CsvFileHandler.exportCsv(exportSettings, store, file);

Writing CSV to a stream

CsvDataStore store = ...
OutputStream os = ...

ExportSettings exportSettings = new ExportSettings();
CsvStreamHandler.exportCsv(exportSettings, store, os);

Getting CSV as a string

CsvDataStore store = ...

ExportSettings exportSettings = new ExportSettings();
String string = CsvStringHandler.exportCsv(exportSettings, store);

Getting CSV as a byte array

CsvDataStore store = ...

ExportSettings exportSettings = new ExportSettings();
byte[] data = CsvByteArrayHandler.exportCsv(exportSettings, store);

Writing CSV to a excel file

File file = new File("/tmp/myfile.xls");
CsvDataStore store = ...

ExportSettings exportSettings = new ExportSettings();
ExcelHandler.exportCsv(exportSettings, store, file);

Getting CSV as HTML

CsvDataStore store = ...

ExportSettings exportSettings = new ExportSettings();
String html = HtmlHandler.exportCsv(exportSettings, store);

Converting data

Due to easyCSV's API, it is very simple to convert CSV data between different storage types. Here's an example:

Read data from database and create a excel file with it

ImportSettings importSettings = new ImportSettings();
ExportSettings exportSettings = new ExportSettings();

PreparedStatement preparedStatement = ...
ResultSet resultSet = preparedStatement.executeQuery();

CsvDataStore store = ResultSetHandler.importCsv(importSettings, resultSet);

File file = new File("/tmp/myfile.xls");
ExcelHandler.exportCsv(exportSettings, store, file);

Advanced examples

Working with different ImportSettings

The ImportSettings class lets you define how the data should be read. You can also plug in custom implementations for header detection and line filtering.

ImportSettings provides the following mutators to change the way how CSV data is to be read:

MutatorDescriptionDefault value
setHeader(boolean header)Defines if the CSV data to read has a header.
If a CsvHeaderDetector is set, this setting
will be ignored.
true
setStartLine(int startLine)All lines before this will be ignored.0 (first line)
setIgnoreEscapes(boolean ignoreEscapes)If this is set to true, all escape characters
found in CSV stream are read as usual data -
they won't be taken as meta characters to
escape field separators.
false
setIgnoreQuotes(boolean ignoreQuotes)If this is set to true, all quotings found in
CSV stream are read as usual data - they won't
be taken as meta characters to enclose fields.
false
setCsvHeaderDetector(CsvHeaderDetector chd)Set a custom header detector implementation,
that will be responsible for automatically
detect if the first line of the CSV stream
is a header or not.
null
setLineFilter(LineFilter lineFilter)Set a filter for filtering out unwanted lines.null
setTrimType(TrimType trimType)Set the way how fields should be trim'ed.
NONE won't trim, LEFT will only trim leading
whitespaces, RIGHT will only trim trailing
whitespaces, BOTH is LEFT and RIGHT combined.
TrimType.BOTH
setNullRepresentation(String nullRepresentation)Set a string that will be treated as null."null"
setSkipEmptyLines(boolean skipEmptyLines)If this is set to true, lines that doesn't
contain data will be ignored (skipped).
true
setFieldEscapeChar(char fieldEscapeChar)Set the character that is used to escape
the field separator.
\240
(escape)
setFieldEncloseChar(char fieldEncloseChar)Set the character that is used to enclose
the fields.
"
(double quote)
setFieldSeparatorChar(char fieldSeperator)Set the character that is used to separate
the fields.
,
(comma)

Working with different ExportSettings

The ExportSettings class lets you define how the data should be written.

ExportSettings provides the following mutators to change the way how CSV data is to be written:

MutatorDescriptionDefault value
setEscaping(boolean escaping)If set to true, the field separation character
will be escaped if it is part of the field
and not intended to separate two fields.
false
setEncloseStrategy(EncloseStrategy eS)As an alternative to character escaping,
fields that contains a separation character
can be enclosed by the enclose character
(usually double quotes).
Valid values are NEVER - fields shouldnt'be
enclosed, OPTIONAL - fields should be enclosed
if necessary, ALWAYS - fields should always
be enclosed by the enclose character.
OPTIONAL
setLineSeparator(String lineSeparator)Defines the new line separator to be used.
The default for this is based on the used
operating system. On Unix, a simple line feed
(LF - 0x0A) is used, on Windows a carriage
return followed by a line feed (CRLF - 0x0D0A)
is used and on Mac systems only a carriage
return (CR - 0x0D) is used.
OS default
setHeader(boolean header)Defines if the CSV header (if one exists)
should be written or not.
true
setTrimType(TrimType trimType)Set the way how fields should be trim'ed.
NONE won't trim, LEFT will only trim leading
whitespaces, RIGHT will only trim trailing
whitespaces, BOTH is LEFT and RIGHT combined.
BOTH
setNullRepresentation(String nullRepresentation)Set a string that should be used for null
values.
"null"
setSkipEmptyLines(boolean skipEmptyLines)If this is set to true, lines that doesn't
contain data will be ignored (skipped).
true
setFieldEscapeChar(char fieldEscapeChar)Set the character that is used to escape
the field separator.
\240
(escape)
setFieldEncloseChar(char fieldEncloseChar)Set the character that is used to enclose
the fields.
"
(double quote)
setFieldSeparatorChar(char fieldSeperator)Set the character that is used to separate
the fields.
,
(comma)