- 浏览: 147625 次
- 性别:
- 来自: 广州
最新评论
-
tianmo2008:
最近在使用,遇到点问题,希望能请教一下.
请问一下,你出现过I ...
RCP中实现带有run in background按钮的进度条对话框 -
zhangzcz1999:
lin36 写道楼主的程序有问题,测试的时间包含有调用&quo ...
java写文件方法之比较 -
hnbcjzj:
FileOutputStream 用于写入诸如图像数据之类的原 ...
java写文件方法之比较 -
zhangzcz1999:
xueshuanglong 写道网上现在的资料尽是一些旧版的, ...
LifeRay安装配置开发环境全过程(一) -
xueshuanglong:
网上现在的资料尽是一些旧版的,没有最新的。
LifeRay安装配置开发环境全过程(一)
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.
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)
If you want to use an Iterator style pattern, you might do something like this:
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:
And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:
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:
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:
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.
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:
For more detailed examples, check out the test cases for each of the available mapping strategies under the /test/au/com/bytecode/opencsv/bean/.
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 one file, 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.
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)
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();
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/.
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 one file, 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.
发表评论
-
(转)Word文档解析介绍(using Jacob & HtmlParser)
2010-11-01 14:21 2681Java中Word的解析方法据我了解有多种,如通过jacob调 ... -
(转)Java同步技术
2010-03-15 15:26 1231Java同步技术 收藏 by:文/ Iangao 200 ... -
Java和oracle分页处理
2009-12-09 18:16 2560Oracle的分页查询语句基 ... -
java写文件方法之比较
2009-11-12 10:57 8586(转)最近对以前开发的一个通用数据迁移的软件进行优化。除了 ... -
Log4j记录详细异常信息
2009-08-11 16:56 1496//代码片断,其中log为log4j对象,ex为异常对象 / ... -
解决iframe中session丢失的问题
2009-08-04 11:35 2802最近由于要做页面引用,在Iframe中引用其他系统的页面,而其 ... -
转:HttpClient POST 的 UTF-8等编码问题
2009-04-26 15:46 3652Apache HttpClient ( http://jaka ... -
RCP中实现带有run in background按钮的进度条对话框
2009-04-02 12:05 3442public class TestWithProgress ... -
spring cron表达式
2009-03-02 15:35 2036Cron表达式是一个字符串 ... -
使用apache的Httpclient上传文件
2009-02-27 14:25 4561String targetUrl = "htt ... -
eclipse中的线程
2009-02-10 16:25 917先大致讲一讲GUI程序中 ... -
GEF初步学习
2009-01-21 17:06 1452这周公司突然要我去接手同事写的GEF和RCP工作。于是急匆匆的 ... -
(转)Java复习
2009-01-16 17:28 804类成员访问修饰符与继承的关系 私有的(private)类成员 ... -
eclipse启动参数(eclipse.ini)说明
2009-01-07 09:45 1680今天在写RCP程序的时候跟踪程序,出现了java.lang.O ... -
Lucene已建索引的全部删除
2008-11-07 17:37 1550今天搞个重建索引工作,一开始想要把建索引的文件目录删除然后再来 ... -
Lucene 搜索试用
2008-10-22 11:57 1007传说中强大的Lucene搜索,首先要创建索引: impor ... -
JXL操作excel代码实例
2008-10-18 17:53 1471import java.io.File; import ... -
Webphere启动报java.lang.ClassCastException问题解决
2008-09-26 19:55 3436昨天使用websphere部署war包,部署成功了,启动也成功 ... -
Tomcat下配置与使用CAS实现单点登录
2008-05-23 10:47 2536配置Tomcat使用SSL安全认证 因为CAS使用的是http ... -
(转)TCP端口扫描程序
2008-05-22 10:17 1279import java.io.IOException; ...
相关推荐
首先,我们需要了解OpenCSV,这是一个开源的C#库,专门用于读写CSV文件。它提供了简单易用的API,使得在C#项目中处理CSV文件变得轻松。在NuGet包管理器中,可以搜索并安装"OpenCSV"库,将其添加到你的项目中。 转换...
- `opencsv`库:这是一个开源的Java库,提供了方便的CSVReader类,简化了读取过程,支持自定义分隔符和处理复杂情况(如包含逗号的字段)。 - `Apache Commons CSV`库:Apache提供的另一个强大的CSV处理库,提供更...
在Java中,读取CSV文件是常见的操作,可以用来处理各种数据输入输出的需求,比如导入导出数据库、分析数据等。 Java本身并不直接支持CSV文件的读写,但可以通过第三方库来实现。描述中提到的“javacsv2.1”是老版本...
- OpenCSV是一个开源的Java库,可以在Maven仓库中找到,通过添加对应的依赖到项目构建文件,如pom.xml,即可引入OpenCSV 2.0。 - `opencsv-2.0.jar` 是包含库的jar文件,而`with.libs.tar`则可能包含了OpenCSV运行...
opencsv 1.8作为一款强大的CSV处理工具,为Java开发者提供了便利的接口,简化了CSV操作流程。无论是在数据分析、数据导入导出还是任何需要与CSV交互的场景,opencsv都是一个值得信赖的选择。通过深入了解和实践,你...
在Java中,我们可以使用Apache Commons CSV库或者OpenCSV库来读取CSV文件。以Apache Commons CSV为例: ```java import org.apache.commons.csv.*; public class CSVReader { public static void main(String[] ...
在Java中,处理CSV文件主要依赖于第三方库,如OpenCSV、Apache Commons CSV或Java 8及以上版本内置的`java.util.CSVPrinter`。在这个程序中,我们可能使用了其中一个库来创建和写入CSV文件。 接下来,我们来探讨...
`javacsv`是一个开源项目,由David Beites创建,它提供了一组简单的Java类,使得开发者可以方便地读取和写入CSV文件。`javacsv`库主要包含两个核心类:`CSVReader`和`CSVWriter`,分别用于读取和写入CSV数据。 1. ...
对于CSV文件,Java标准库没有内置的支持,但我们可以使用开源库如OpenCSV或Apache Commons CSV。解析CSV的基本步骤如下: 1. 创建CSVReader,传入文件流或文件路径。 2. 使用`readNext()`方法逐行读取CSV数据。 3. ...
通常,开发者只需将此JAR文件添加到项目的类路径中,就可以直接通过`import`语句引入所需的类,如`com.opencsv.CSVReader`和`com.opencsv.CSVWriter`,进而进行CSV操作。 使用OpenCSV的具体步骤大致如下: 1. **...
"javacsv"库由OpenCSV项目提供,是一个轻量级、高性能的开源Java库,专门设计用来处理CSV数据。它支持多种CSV文件格式,包括那些使用自定义分隔符(如制表符、分号等)或包含复杂结构(如Excel风格的引用)的文件。...
4. **错误处理**:库提供了处理文件不存在、读取异常或格式错误的机制,帮助开发者更健壮地处理CSV操作。 5. **内存效率**:考虑到CSV文件可能很大,`javacsv.jar`设计时考虑了内存效率,避免一次性加载整个文件到...
OpenCSV是Java的一个开源库,专门用于读写CSV文件,它提供了简单易用的API,使开发者能够方便地处理CSV数据。 **OpenCSV的基本使用:** 1. **引入依赖**:在项目中添加OpenCSV的依赖,如果是Maven项目,可以在pom....
在这个名为"matlab开发-csvreader"的压缩包中,包含了一些关键组件和文档,旨在简化CSV文件的读取操作。 首先,`csvreader.m`是核心功能文件,它实现了CSV文件的解析逻辑。该函数可能包含了自定义的算法或方法,...
OpenCSV,一个强大的开源Java库,为我们提供了方便、高效的CSV解析和生成功能。本文将详细探讨OpenCSV 1.8版本的功能特性、使用方法以及其在实际项目中的应用。 OpenCSV是Java社区中的一款明星工具,它的主要目标是...
Java中处理CSV文件,通常可以使用开源库,如Apache Commons CSV、OpenCSV或uniVocity的csv-parser。这些库提供API来轻松地读取、解析和操作CSV数据。以Apache Commons CSV为例,我们可以创建一个`CSVParser`实例,...
CSVFileReader是一个专门为处理CSV(逗号分隔值)文件设计的Java类,它使得...通过理解和运用这些知识点,你可以根据项目需求定制自己的CSVFileReader实现,或者利用现有的开源库如Apache Commons CSV、OpenCSV等。
这里我们关注的是"CsvUtils工具",它可能是某个开源项目或个人开发的针对CSV文件操作的实用工具类。 博客链接中的文章《CsvUtils工具详解》提供了关于这个工具的详细信息。虽然具体内容未直接给出,但我们可以根据...
OpenCSV是处理CSV(逗号分隔值)文件的库,虽然不是专门处理Excel,但CSV是Excel文件的一种常见导出格式,因此在某些场景下也可以用来处理Excel数据。 这些JAR包的导入过程通常包括以下步骤: 1. 将压缩包解压,...
标题中的"opencsv-jdk6:OpenCSV http的分支"指的是一个针对Java开发的开源CSV解析库OpenCSV的一个特定版本,它被修改以兼容Java Development Kit (JDK) 6。OpenCSV是一个广泛使用的库,它允许Java开发者方便地读写...