Utilities for reading and writing CSV (comma separated value) text files.
The CSV parser classes take care of reading CSV files including: splitting out fields (even quoted and escaped fields), escaped data, line numbering, escape and comments. Each line of values can be returned as an array, or the values can be returned individually with the number of the line from which they came.
The CSV printer classes take care of writing CSV files including determining if the data needs to be escaped.
This utility supports two flavors of CSV:
Note that for simple field data that does not contain quotes or new lines, the two formats are fairly equivalent.
Decorates either any flavor of CSV parser such that the first row read is a row of labels. Data in each row can be read by column name.
LabeledCSVParser lcsvp = new LabeledCSVParser(
new CSVParser(
new StringReader(
"Name,Phone\n" +
"Stewart,212-555-3233\n" +
"Cindy,212-555-8492\n"
)
)
);
while(lcsvp.getLine() != null){
System.out.println(
"Name: " + lcsvp.getValueByLabel("Name")
);
System.out.println(
"Phone: " + lcsvp.getValueByLabel("Phone")
);
}
Common interface for either the UNIX style CSV reader (CSVParser) or the Excel style CSV reader (ExcelCSVParser).
Common interface for either the UNIX style CSV writer (CSVPrinter) or the Excel style CSV writer (ExcelCSVPrinter).
This class makes it easy to output CSV. Given values, it will automatically determine if they need to be quoted and escape special characters. Comments can easily be written and system appropriate line beginnings will be added.
This class makes it easy to input CSV. Given a stream, it will parse the stream and return the fields as strings, the lines as string arrays, or the whole file as a two dimensional string array.
Several people have asked how to read CSV files that are in other character sets such as Chinese or Japanese. To parse such files, use the parser constructor that takes a reader. Make sure the reader has been initialized to read the correct character set. The following example reads Simplified Chinese (charset GB2312) CSV values from CSVCharsetTest.gb2312csv If you have a Chinese font installed and Java is set up to use it, the example will show a dialog with each of the Chinese words on it.
CSVParser shredder = new CSVParser(
new InputStreamReader(
new FileInputStream("CSVCharsetTest.gb2312csv"),
"GB2312"
)
);
String t;
while ((t = shredder.nextValue()) != null) {
JOptionPane.showMessageDialog(
null,
t,
"Value from line " + shredder.getLastLineNumber(),
JOptionPane.INFORMATION_MESSAGE
);
}
The lexer (CSVLexer) created using JFlex is still available in the download and is still supported. In fact, CSVParser uses it behind the scenes. However, CSVParser has a much cleaner, full-featured API and its use is recommended.
| Author | License | Features |
| Stephen Ostermiller ostermillerutils CSV and ExcelCSV for Java | Open source, GPL | Parses CSV streams into Java Strings or arrays of Strings. |
| Bytecode Pty Ltd. opencsv | Open source, Apache licence | Parses CSV files into Java Strings or arrays of Strings. |
| Ricebridge CSV Manager configured to parse standard CSV, Excel CSV, or other user specified variants. | Commercial, with various license price points. | Parses CSV streams with callback methods when data is found. Single CSV parsing class can be |
| E.Allen Soard Java CSV Library | Open source, LGPL | Parses CSV files into Java objects contained entirely in memory. |
| Nilo de Roock xlSQL | Open source, GPL | Provides a JDBC interface for accessing CSV files. |
| Bruce Dunwiddie CsvReader | Commercial, with various license price points. | Reads CSV files one line at a time and values may be obtained by name (similar to LabeledCSVParser) or by index. Single CSV parsing class can be configured to parse standard CSV, Excel CSV, or other user specified variants. A .Net version of the parser is also available. |
| Roedy Green Mindprod CSV | Open source, freeware (except military) | CSV definition and libraries. |