|
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: CSVPrinterThis 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: CSVParserJava'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 SetsSeveral 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. CSVLexerThe 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
|
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?