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:
-
CSVReader reader =
new
CSVReader(
new
FileReader(
"yourfile.csv"
));
-
String [] nextLine;
-
while
((nextLine = reader.readNext()) !=
null
) {
-
-
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();
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'
);
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'
,
'\''
);
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
);
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'
);
-
-
String[] entries = "first#second#third"
.split(
"#"
);
-
writer.writeNext(entries);
-
writer.close();
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);
-
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"
};
-
strat.setColumnMapping(columns);
-
-
CsvToBean csv = new
CsvToBean();
-
List list = csv.parse(strat, yourReader);
-
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.
分享到:
相关推荐
首先,我们需要了解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开发者方便地读写...