Java读取csv文件

  1. package com.thinkive.common.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.util.ArrayList;  
  8. import java.util.List;  
  9.   
  10. import com.thinkive.common.function.gastatisics.bean.Bean;  
  11.   
  12. /** 
  13.  * @desc: 读取csv文件, 并将内容插入到数据库 
  14.  * @author: changez@thinkive.com 
  15.  * @time: 2016年11月28日 下午3:37:35 
  16.  */  
  17. public class ReadCsv {  
  18.   
  19.     public static void readCsvAndInstallDB(String path, int ignoreRows) throws Exception {  
  20.         File file = new File(path);  
  21.         BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), Constants.CHAR_CODING_GBK));  
  22.         String line = "";  
  23.         List<Bean> beanList = new ArrayList<Bean>();  
  24.         int count = 0;  
  25.         // 忽略前几行标题  
  26.         if(ignoreRows > 0) {  
  27.             for (int i = 0; i < ignoreRows; i++) {  
  28.                 line = bReader.readLine();  
  29.             }  
  30.         }  
  31.         try {  
  32.             while((line = bReader.readLine()) != null) {  
  33.     //          System.out.println(++count+"  "+line);  
  34.                 if(line.trim() != "") {  
  35.                     String[] pills = line.split(",");  
  36.                     Bean bean = new Bean(pills[0].trim(), pills[1].trim(), pills[2].trim(), pills[3].trim(), Constants.CHANNEL_TYPE_GUI_TAI);  
  37.                     beanList.add(bean);  
  38.                     if(++count%Constants.BATCH_NUM == 0) {  
  39.                         // 数据库操作, 见“jdbc批量插入一文”  
  40.                         DBHelp.executeUpate(DBHelp.SQL_INSTALL_IDNO_THIRD, beanList, Constants.DATE_FORMATE_DEFAULT);  
  41.                         beanList.clear();  
  42.                     }  
  43.                 }  
  44.             }  
  45.             // 操作集合中最后一批数据  <span style="font-family: Arial, Helvetica, sans-serif;">数据库操作, 见“jdbc批量插入一文”</span>  
  46.             DBHelp.executeUpate(DBHelp.SQL_INSTALL_IDNO_THIRD, beanList, Constants.DATE_FORMATE_DEFAULT);  
  47.             beanList.clear();  
  48.             DBHelp.closeSources(DBHelp.getConn(), DBHelp.getPs());  
  49.             beanList = null;  
  50.         }finally {  
  51.             if(bReader != null) {  
  52.                 bReader.close();  
  53.             }  
  54.         }  
  55.     }  
  56. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值