首先在http://ostermiller.org/utils/download.html 上下载com.Ostermiller.util cvs的jar包.
有了这个jar包就可以写个工具类,专门控制csv文件的读取操作.
具体代码如下:
public class CsvFileParser{
private LabeledCSVParser csvParser;//csvParser
private int currLineNum = -1;//The current line number for reading.
private String[] currLine = null;//The data of current line.
/*
* Param: in InputStream
* throws IOException
*/
public CsvFileParser(InputStream in) throws IOException {
csvParser = new LabeledCSVParser(new ExcelCSVParser(in));
currLineNum = csvParser.getLastLineNumber();
}
/*
* Get the titles of csv file.
*
* return String[]
*/
public String[] getTitles() throws IOException {
return csvParser.getLabels();
}
/*
* Checkout whether it has more data.
*
* @return ture: has data,false nodata.
*/
public boolean hasMore() throws IOException {
currLine = csvParser.getLine();
currLineNum = csvParser.getLastLineNumber();
if (null == currLine)
return false;
return true;
}
/*
* Get the data of filed by field name.
* param:String filedName
*
* @return:String
*/
public String getByFieldName(String fieldName) {
return csvParser.getValueByLabel(fieldName);
}
/*
* Close the parser.
*
*
*/
public void close() throws IOException {
csvParser.close();
}
/*
* Read the current line.Return the data of current line.
*
* @return String[]
*/
public String[] readLine() throws IOException {
currLine = csvParser.getLine();
currLineNum = csvParser.getLastLineNumber();
return currLine;
}
/*
* Get current line number.
*
* @return int
*/
public int getCurrLineNum(){
return currLineNum;
}
/*
* Get the data of the current line.
*
* @return String[]
*/
public String[] getCurrLine() {
return currLine;
}
}
Ok,有其他需要可以看看那个jar包官网的doc.
分享到:
相关推荐
在我们需求中需要读取一个csv文件的数据,但是只有旧的csv的几列数据再新写入一个csv文件,而且在旧的数据有空的数据,需要判断旧的csv的空值。可能写的存在不完善!但是应用的需求里面还是能用的
本人以JAVA来实现以支付宝的账单表为例,实现JAVA读取CSV..csv是一种文件格式(如.txt、.doc等),也可理解.csv文件就是一种特殊格式的纯文本文件。即是一组字符序列,字符之间已英文字符的逗号或制表符(Tab)分隔。
NULL 博文链接:https://qq-24665727.iteye.com/blog/2339850
java 语言读取csv文件,避免导入Excel时出现的单元格取值的问题
Java读取excel,Excel是我们平时工作中比较常用的用于存储二维表数据的,JAVA也可以直接对Excel进行操作。
通过引用【opevcsv】依赖,逐行读取数据 并转为Map,Object>格式,后续可以通过BeanUtil.MaptoBean()方法...csv文件必须带表头,bean对象命名符合驼峰格式; 通过此方法可以快速的将文件转换为程序可以操作的bean对象;
主要介绍了java读取csv文件示例,这个java解析csv文件的例子很简单,下面直接上代码,大家参考使用吧
NULL 博文链接:https://thinktothings.iteye.com/blog/1536044
java 解析csv文件例子,csv文件 中文乱码问题
通过Java读取指定行列的数据,请用eclipse平台打开。实现思想详见我的博客。
可读取复杂的csv文件,根据引号和逗号等分割读取所需要的数据信息。
下载javacsv-2.0.jar ,根据例子进行读写CSV操作。
利用Java swing 对csv和excel文件进行读写;并且可以同时读取5个文件的数量;通过关键字配置来读文件,还有就是通过POI CSVWriter等类的使用来对excel和csv等文件进行操作
该文档主要介绍java如何操作csv文件,在这里介绍了一个工具包的使用,也黏贴了java对于读取csv文件和写入csv文件时,对于大数据量的处理和安排,有效的解决了处理大数据是内存溢出问题
java读取csv格式文件jar包,已经修改过bug.
使用java对csv文件进行读写操作的源代码,包含javacsv.jar
一个非常好用的csv文件操作工具
主要介绍了java读取csv文件内容的示例,大家参考使用
通过java解析上传文件,判断文件类型(excel或者csv),将解析后的数据映射到javabean,从而可以进一步操作,入库或者增添数据