opencsv

转载 2011年09月15日 22:19:44

General

What is opencsv?
opencsv is a very simple csv (comma-separated values) parser library for Java. It was developed because all of current csv parsers I've come across don't have commercial-friendly licenses.
Where can I get it?
Source and binaries are available from Sourceforge. You can check out the javadocs online.
What features does opencsv support?
opencsv supports all the basic csv-type things you're likely to want to do:
  • Arbitrary numbers of values per line
  • Ignoring commas in quoted elements
  • Handling quoted entries with embedded carriage returns (ie entries that span multiple lines)
  • Configurable separator and quote characters (or use sensible defaults)
  • Read all the entries at once, or use an Iterator style model
  • Creating csv files from String[] (ie. automatic escaping of embedded quote chars)

Reading and Writing

How do I read and parse a CSV file?
If you want to use an Iterator style pattern, you might do something like this:
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
    String [] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        // nextLine[] is an array of values from the line
        System.out.println(nextLine[0] + nextLine[1] + "etc...");
    }
           
Or, if you might just want to slurp the whole lot into a List, just call readAll()...
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"));
    List myEntries = reader.readAll();
           
which will give you a List of String[] that you can iterate over. If all else fails, check out the Javadoc.
Can I use my own separators and quote characters?
Yes. There are constructors that cater for supplying your own separator and quote characters. Say you're using a tab for your separator, you can do something like this:
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');
               
And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');
           
You may also skip the first few lines of the file if you know that the content doesn't start till later in the file. So, for example, you can skip the first two lines by doing:
    CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'', 2);
           

Can I write csv files with opencsv?
Yes. There is a CSVWriter in the same package that follows the same semantics as the CSVReader. For example, to write a tab separated file:
     CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
     // feed in your array (or convert your data to an array)
     String[] entries = "first#second#third".split("#");
     writer.writeNext(entries);
	writer.close();
               

If you'd prefer to use your own quote characters, you may use the three arg version of the constructor, which takes a quote character (or feel free to pass in CSVWriter.NO_QUOTE_CHARACTER).

You can also customise the line terminators used in the generated file (which is handy when you're exporting from your Linux web application to Windows clients). There is a constructor argument for this purpose.
Can I dump out SQL tables to CSV?
Yes you can. Sean Sullivan added a neat feature to CSVWriter so you can pass writeAll() a ResultSet.
java.sql.ResultSet myResultSet = ....
writer.writeAll(myResultSet, includeHeaders);
           

Is there a way to bind my CSV file to a list of Javabeans?
Yes there is. Kyle Miller added a new set of classes to allow you to bind a CSV file to a list of JavaBeans based on column name, column position, or a custom mapping strategy. You can find the new classes in the au.com.bytecode.opencsv.bean package. Here's how you can map to a java bean based on the field positions in your CSV file:
ColumnPositionMappingStrategy strat = new ColumnPositionMappingStrategy();
strat.setType(YourOrderBean.class);
String[] columns = new String[] {"name", "orderNumber", "id"}; // the fields to bind do in your JavaBean
strat.setColumnMapping(columns);

CsvToBean csv = new CsvToBean();
List list = csv.parse(strat, yourReader);
           
For more detailed examples, check out the test cases for each of the available mapping strategies under the /test/au/com/bytecode/opencsv/bean/.

Other Stuff

Can I use opencsv in my commercial applications?
Yes. opencsv is available under a commercial-friendly Apache 2.0 license. You are free to include it in your commericial applications without any fee or charge, and you are free to modify it to suit your circumstances. To find out more details of the license, read the Apache 2.0 license agreement.
Can I get the source? More example code?
Yes. The download from the SourceForge page includes the full source in the /src directory. It's just a few files, so go crazy. There is also a sample addressbook csv reader in the /examples directory. And for extra marks, there's a JUnit test suite in the /test directory.
How can I use it in my Maven projects?
Add a dependency element to your pom:
<dependency>
	<groupId>net.sf.opencsv</groupId>
	<artifactId>opencsv</artifactId>
	<version>2.0</version>
</dependency>
				

Who maintains opencsv?
opencsv was developed in a couple of hours by Glen Smith. You can read his blog for more info and contact details. Kyle Miller contributed the bean binding work, Scott Conway has done tons of bug fixing, and Sean Sullivan is the current maintainer of the project.

If you've found a bug, you can report it on the project page at Sourceforge. Please post a sample file that demonstrates your issue. For bonus marks, post a patch too. :-)

opencsv Development Stuff

Is there a snapshot Maven repo for opencsv?
Yes. We're using a repo provided by the good folk at Sonatype.
I'm an opencsv developer, how do you go about releasing a new version?
Here's the general process for a release:
  • First you talk to everyone else on the team to make sure they're on the same page.
  • After that you can follow the detailed instructions found on the Sonatype site to get a JIRA username/password
  • As part of that process, you'll need to add your JIRA account name as a comment to our existing JIRA ticket.
  • Once you have access, change your Maven settings.xml on your local machine per Step 7a.1. in the Sonatype setup doc.
  • With all that in place, you can do a "mvn deploy" to release snapshots, or if you're ready to release, create a new branch, set the version in the POM, and "mvn release" again.
  • For prod releases, the release process puts things in the staging Nexus at Sonatype. It's a manual step from there to release (see step 8 of the Sonatype setup doc). First you have to "Close" the release in the staging repo (which checks the signatures and metadata), then they sent you an email, then you have to "Release" the release to central.
  • Finally, update the Sourceforge website with the latest Javadoc by using a "mvn site:deploy" (Details here)

opencsv方式读取csv文件,详解,示例

1.opencsv官网:http://opencsv.sourceforge.net/    jar包:opencsv-2.3.jar    下载地址:http://sourceforge.net/p...
  • han_huayi
  • han_huayi
  • 2017年02月17日 13:25
  • 2024

openCSV读写CSV文件

使用openCSV读写CSV文件的方法和例子,需注意UTF-8的BOM标识。可自动映射csv文件与javabean,方便读写操作。...
  • peterwanghao
  • peterwanghao
  • 2017年07月19日 16:44
  • 994

Java opencsv 生成、读取、下载、删除CSV文件

1. 利用opencsv包中的CSVWriter类,生成csv文件 import java.io.File; import java.io.FileOutputStream; import ja...
  • cat_book_milk
  • cat_book_milk
  • 2016年10月28日 10:08
  • 2434

openCsv通用导出

通用openCsv工具类
  • paincupid
  • paincupid
  • 2016年08月28日 21:53
  • 1072

Java+Selenium3方法篇44-利用opencsv读取csv文件

前面介绍了如何读取excel文件,本篇介绍如何读取vsc文件,什么是csv,它的定义是与逗号分隔的值(Comma-Separated Values),同样的Java中需要用到第三方lib去处理读取cs...
  • u011541946
  • u011541946
  • 2017年07月09日 23:27
  • 861

Java:读写CSV格式文件(opencsv)

使用opencsv(opencsv-2.3.jar),下载地址:http://sourceforge.net/projects/opencsv/files/latest/download 一,读取CS...
  • qq592304796
  • qq592304796
  • 2016年10月12日 21:48
  • 440

用opencsv和用Bufferedreader/writer进行csv文件的读写及简单处理

最近要大量处理一批.csv文件,由于收集到的数据大部分是9列,但是有的行的列数大于9,因此想写个程序去批处理这些.csv文件,使得处理好的文件可以是规则的,方便导入数据库。 方法一: 首先我想到了...
  • YQlakers
  • YQlakers
  • 2017年04月21日 17:31
  • 651

Learning Spark笔记11-加载保存数据

5.加载,然后保存数据 工程师希望发现更多的输出格式来适合下游的消费者。数据科学家可能关注数据已经存在的格式。 5.1动机 Spark提供了一个各种各样输入输出数据源的操...
  • zhuziying99
  • zhuziying99
  • 2017年07月26日 11:39
  • 122

用opencsv文件读写CSV文件

首先明白csv文件长啥样儿: 用excel打开就变成表格了,看不到细节推荐用其它简单粗暴一点儿的编辑器,比如Notepad++, csv文件内容如下: csv文件默认用逗号分...
  • v1t1p9hvbd
  • v1t1p9hvbd
  • 2017年05月18日 13:46
  • 255

Java读写CSV格式文件(opencsv)

文章转自:http://blog.csdn.net/a19881029/article/details/37959109 使用opencsv(opencsv-2.3.jar),下载地址:http...
  • a1259109679
  • a1259109679
  • 2015年06月19日 18:35
  • 269
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:opencsv
举报原因:
原因补充:

(最多只允许输入30个字)