Ostermillerutil Java Utilities Comma Separated Values (CSV) - com.Ostermiller.util Java Utilities

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.

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

AuthorLicenseFeatures
Stephen Ostermiller
ostermillerutils CSV and ExcelCSV for Java
Open source, GPL Parses CSV streams into Java Strings or arrays of Strings.
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.

License

OstermillerUtil Java Utilities Copyright (c) 2001-2020 by Stephen Ostermiller and other contributors

The OstermillerUtils library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

License FAQs - Why GPL? How about the LGPL or something else?