%bte.doc super="item.bte" %> <%bte.tpl name=pageTitle%>Comma Separated Values (CSV)<%/bte.tpl%> <%bte.tpl name=description%>Java libraries to read and write files in Comma Separated Value (CSV) format.<%/bte.tpl%> <%bte.tpl name=keywords%>csv, comma separated values, java csv library, library for csv, utilities csv java, csv file format java<%/bte.tpl%> <%bte.tpl name=content%>
// Create the printer CSVPrinter csvp = new CSVPrinter( System.out ); // Write to the printer csvp.writeln( new String[]{ "hello","world" } );
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 correct line beginnings will be added.
Some applications do not accept CSV input according to the generally accepted standards. One such application is the Microsoft Excel spreadsheet. A separate class must be use to write Excel CSV. Both CSVPrinter and ExcelCSVPrinter implement the CSVPrint interface.
[Download /w Source | Version History | Browse Source | Documentation]
// Parse the data String[][] values = CSVParser.parse( new StringReader( "hello,world\n" + "how,are,you" ) ); // Display the parsed data for (int i=0; i<values.length; i++){ for (int j=0; j<values[i].length; j++){ System.out.println(values[i][j]); } System.out.println("-----"); }
Java's StringTokenizer does not make it easy to parse files of comma separated values for two reasons. First StringTokenizer doesn't handle empty strings and second it doesn't have a way to easily get Strings in quotes that have commas inside them. This CSV parser takes care of those issues and support line numbering, escape characters, 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.
Some applications do not output CSV according to the generally accepted standards and this parse may not be able to handle it. One such application is the Microsoft Excel spreadsheet. A separate class must be use to read Excel CSV. Both CSVParser and ExcelCSVParser implement the CSVParse interface.
If the first line of your CSV file is a row of column headings, consider wrapping this parser in a Labeled CSV Parser.
[Download /w Source | Version History | Browse Source | Documentation]
Several people have asked how to read CSV files that are in other character sets such as Chinese or Japanese. To parse such files, simple use the CSVParser constructor that takes a reader. Make sure the reader has been initialized to read the correct character set. An example that reads Simplified Chinese (charset GB2312) CSV values from CSVCharsetTest.gb2312csv can be found in CSVCharsetTest.java. 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.
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.
A CSVLexer regression test and the expected results of that test are available.
Author | License | Features |
---|---|---|
Stephen Ostermiller ostermillerutils CSV and ExcelCSV for Java | Open source, GPL | Parses CSV streams into Java Strings or arrays of Strings. |
Ricebridge CSV Manager | Commercial, with various license price points. | Parses CSV streams with callback methods when data is found. Single CSV parsing class can be configured to parse standard CSV, Excel CSV, or other user specified variants. |
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. |