com.Ostermiller.util.CSV.bte Maven / Gradle / Ivy
<%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=topcontent%>
Utilities for reading and writing CSV (comma separated value) text files. CSV as supported by these classes uses backslashes to escape quotes and new lines. For CSV that is compatible with Microsoft Excel, separate Excel CSV libraries are available.
<%/bte.tpl%>
<%bte.tpl name=content%>
Example
// Create the printer
CSVPrinter csvp = new CSVPrinter(
System.out
);
// Write to the printer
csvp.writeln(
new String[]{
"hello","world"
}
);
Writing CSV files: CSVPrinter
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.
Example
// 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("-----");
}
Reading CSV files: CSVParser
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.
CSV Character Sets
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.
CSVLexer
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.
Links
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.
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.
<%/bte.tpl%>
<%bte.tpl name=linkCSV%>Comma Separated Values (CSV)
<%/bte.tpl%>
<%/bte.doc%>