Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2027025
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: Java

2008-05-17 23:01:22

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, 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 . 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("-----");
}

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 . 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

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 can be found in . 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 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 and the expected of that test are available.

Links

Author License Features
Stephen Ostermiller
Open source, GPL Parses CSV streams into Java Strings or arrays of Strings.
Ricebridge
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
Open source, LGPL Parses CSV files into Java objects contained entirely in memory.
Nilo de Roock
Open source, GPL Provides a JDBC interface for accessing CSV files.
Bruce Dunwiddie
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
Open source, freeware (except military) CSV definition and libraries.
阅读(1331) | 评论(0) | 转发(0) |
0

上一篇:csv文件的读出

下一篇:Programming

给主人留下些什么吧!~~