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();
}
}