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.
分享到:
相关推荐
从雅虎金融上面自动下载csv文件,供量化和分析使用 修改save_path为自己的本地地址,输入股票代码,即可下载。
NULL 博文链接:https://woniu1983.iteye.com/blog/693251
uncsv 是一个过滤命令,把 CSV 文件的行转换为非转义、非引用的分隔符文件 (默认为 pipe )。如果遇到一个字符匹配的分隔符就会出错,而且会取代回车和新行中引用的字符。uncsv 允许你使用 awk 来运行你的 CSVs。 csv...
CSVboard 是一种以最少的努力加载 CSV 文件的工具。 由于我非常重视它的易用性,因此我实现了一个搜索和过滤引擎,它为您提供了在表中高效查找指定行的便利。 不要忘记阅读快速教程!! 特点: 轻巧便携 使用 Ctrl+q...
csv.vim, csv文件的文件类型插件 简介这个插件用于处理带有Vim的列分离数据。 通常这些文件称为csv文件并使用','作为分隔符,虽然有时它们使用 比如 作为分隔符,也可以使用作为分隔符,但也存在fixedWidth列 ...
semantic-csv, 用于处理CSV数据和文件的高级工具 语义 CSV 为高级CSV解析/过程功能提供一个Clojure库。 Clojure clojure/data.csv parsing目前最流行的两个CSV解析库concern只关注CSV和 clojure-csv,它们都是CSV的...
easy-csv, 用于读取和写入CSV文件的PHP 5.4类的集合 EasyCSVEasyCSV是面向 PHP 5.4 的简单面向对象的CSV操作库 安装通过 Composer 安装:composer require jwage/easy-csv阅读器要读
LINQtoCSV, 流行,易于使用的库读取和写入CSV文件 LINQtoCSV这个库使得在LINQ查询中使用CSV文件变得容易。 它的功能包括:遵循CSV文件最常见的规则。 正确处理包含逗号和换行符的数据字段。除逗号外,还可以使用...
可以说CSV格式的文件经常碰到,何为CSV格式,CSV全称comma-separated values,就是典型的用逗号隔开的文件,比如下面这种文件格式 ...Java开源框架CSVReader提供了一个轻量级、简单方便的统一操作接口可用,
前端开源库-csv-stringcsv字符串,对csv字符串进行分析和字符串化。它类似于JSON对象,但对于csv。它还可以一行一行地工作。而且,如果可以解析字符串,它也可以用于解析文件或流。
opencsv源码包,含有例子,含有jar包等,是一种方便的java使用csv的开源包
java开源的文件管理系统,可以学习学习
该程序允许用户使用 SQL 命令修改 CSV 文件。 该程序使用临时 H2 数据库来包含 CSV 文件,并接受 SQL 命令。 然后,您可以将其保存回 CSV 文件,而不会扭曲任何数据。 此项目仅支持 CSV 逗号分隔文件。 目前不支持...
super-csv:一个快速、程序员友好、开源的CSV读写Java库
查看器(大)CSV文件。 该程序在以下情况下很有用:-您要预览CSV文件。 -该文件对于Microsoft Excel或LibreOffice Calc太大,或者在这些程序之一中打开文件花费了太多时间。 荞麦CSV:-不会将整个文件加载到内存中。...
csv-fingerprint, CSV文件数据类型的定性可视化 CSV指纹CSV指纹是生成任何CSV文件的定性可视化的小应用程序,帮助调试任何格式问题。 每个单元格根据它的类型着色。 对于字符串,为黄色,对于整数为蓝色,对于小数为...
Sublime-Text-Advanced-CSV, 在CSV文件中,有效地格式化,编辑,排列和评估单元格 sublime-text 高级 CSVmanage Sublime-Text-2-CSV-Plugin文件的插件,从的 ...的fo
clojure-csv, 从Clojure读取和写入CSV文件的库 csvclojure CSV是一个用于读取和写入CSV文件的小型库。 主要功能:接受两个常见行终止符。CSV字段内的引号和转义符是正确的句柄。解析时支持在CSV字段中嵌入未转义的行...
csv2json, 一个 gem,用于从 命令行 将CSV文件转换为 JSON csv2json客户端正在向我发送XLS文件,但我的网站使用 JSONs 。...解决方案导出XLS为CSV文件( 我使用 OpenOffice.org 进行这里操作)运行 csv2