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:
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.
File file = new File("/tmp/myfile.csv");
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = CsvFileHandler.importCsv(importSettings, file);InputStream is = ... ImportSettings importSettings = new ImportSettings(); CsvDataStore store = CsvStreamHandler.importCsv(importSettings, is);
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);byte[] data = ... ImportSettings importSettings = new ImportSettings(); CsvDataStore store = CsvByteArrayHandler.importCsv(importSettings, data);
File file = new File("/tmp/myfile.xls");
ImportSettings importSettings = new ImportSettings();
CsvDataStore store = ExcelHandler.importCsv(importSettings, file);PreparedStatement preparedStatement = ... ResultSet resultSet = preparedStatement.executeQuery(); ImportSettings importSettings = new ImportSettings(); CsvDataStore store = ResultSetHandler.importCsv(importSettings, resultSet);
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.
CsvDataStore store = ...
File file = new File("/tmp/myfile.csv");
ExportSettings exportSettings = new ExportSettings();
CsvFileHandler.exportCsv(exportSettings, store, file);CsvDataStore store = ... OutputStream os = ... ExportSettings exportSettings = new ExportSettings(); CsvStreamHandler.exportCsv(exportSettings, store, os);
CsvDataStore store = ... ExportSettings exportSettings = new ExportSettings(); String string = CsvStringHandler.exportCsv(exportSettings, store);
CsvDataStore store = ... ExportSettings exportSettings = new ExportSettings(); byte[] data = CsvByteArrayHandler.exportCsv(exportSettings, store);
File file = new File("/tmp/myfile.xls");
CsvDataStore store = ...
ExportSettings exportSettings = new ExportSettings();
ExcelHandler.exportCsv(exportSettings, store, file);CsvDataStore store = ... ExportSettings exportSettings = new ExportSettings(); String html = HtmlHandler.exportCsv(exportSettings, store);
Due to easyCSV's API, it is very simple to convert CSV data between different storage types. Here's an example:
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);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:
| Mutator | Description | Default 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) |
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:
| Mutator | Description | Default 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) |