首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

JAVA 读取CSV资料

2012-09-09 
JAVA 读取CSV文件1、读取CSV文件只从第二行开始读取,忽略标题的,包文件自己搜索下载import java.io.FileInp

JAVA 读取CSV文件

1、读取CSV文件只从第二行开始读取,忽略标题的,包文件自己搜索下载

import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import com.Ostermiller.util.ExcelCSVParser;import com.Ostermiller.util.LabeledCSVParser;public class CsvImporter {private LabeledCSVParser csvParser;private FileInputStream fileinput;private InputStreamReader reader;public CsvImporter(String filePath) throws Exception {fileinput = new FileInputStream(filePath);reader = new InputStreamReader(fileinput, "gbk");csvParser = new LabeledCSVParser(new ExcelCSVParser(reader));}public List<ProductInfo> nextRows(int startPoint,int count) throws IOException {List<ProductInfo> resList = new ArrayList<ProductInfo>();for (int i = startPoint; i < count; i++) {String[] res = csvParser.getLine();if (res == null || res.length <= 0) {return resList;}else{ProductInfo productInfo = new ProductInfo();productInfo.setId(res[0]);productInfo.setProductId(res[1]);productInfo.setProductCode(res[2]);productInfo.setProductName(res[3]);productInfo.setScriptCode(res[4]);resList.add(productInfo);}}return resList;}public String[] getLine() throws IOException{return csvParser.getLine();}public void closeStream() throws IOException {fileinput.close();reader.close();csvParser.close();}}

热点排行