Java读取csv文件

 

要做个批量导入,但是要暂停了,先放这儿,别丢了

  
  
  
  
  1. package com.huateng.readcsv; 
  2.   
  3. import java.io.BufferedReader; 
  4. import java.io.FileReader; 
  5. import java.util.ArrayList; 
  6. import java.util.Iterator; 
  7. import java.util.List; 
  8.   
  9. public class CsvUtil { 
  10.         private String fileName = null
  11.         private BufferedReader br = null
  12.         private List<String> list = new ArrayList<String>(); 
  13.   
  14.         public CsvUtil() { 
  15.   
  16.         } 
  17.   
  18.         public CsvUtil(String fileName) throws Exception { 
  19.                 this.fileName = fileName; 
  20.                 br = new BufferedReader(new FileReader(fileName)); 
  21.                 String stemp; 
  22.                 while ((stemp = br.readLine()) != null) { 
  23.                         list.add(stemp); 
  24.                 } 
  25.         } 
  26.   
  27.         public List getList() { 
  28.                 return list; 
  29.         } 
  30.         /** 
  31.          * 获取行数 
  32.          * @return 
  33.          */ 
  34.         public int getRowNum() { 
  35.                 return list.size(); 
  36.         } 
  37.         /** 
  38.          * 获取列数 
  39.          * @return 
  40.          */ 
  41.         public int getColNum() { 
  42.                 if (!list.toString().equals("[]")) { 
  43.                         if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件 
  44.                                 return list.get(0).toString().split(",").length; 
  45.                         } else if (list.get(0).toString().trim().length() != 0) { 
  46.                                 return 1
  47.                         } else { 
  48.                                 return 0
  49.                         } 
  50.                 } else { 
  51.                         return 0
  52.                 } 
  53.         } 
  54.         /** 
  55.          * 获取制定行 
  56.          * @param index 
  57.          * @return 
  58.          */ 
  59.         public String getRow(int index) { 
  60.                 if (this.list.size() != 0) { 
  61.                         return (String) list.get(index); 
  62.                 } else { 
  63.                         return null
  64.                 } 
  65.         } 
  66.         /** 
  67.          * 获取指定列 
  68.          * @param index 
  69.          * @return 
  70.          */ 
  71.         public String getCol(int index) { 
  72.                 if (this.getColNum() == 0) { 
  73.                         return null
  74.                 } 
  75.                 StringBuffer sb = new StringBuffer(); 
  76.                 String tmp = null
  77.                 int colnum = this.getColNum(); 
  78.                 if (colnum > 1) { 
  79.                         for (Iterator it = list.iterator(); it.hasNext();) { 
  80.                                 tmp = it.next().toString(); 
  81.                                 sb = sb.append(tmp.split(",")[index] + ","); 
  82.                         } 
  83.                 } else { 
  84.                         for (Iterator it = list.iterator(); it.hasNext();) { 
  85.                                 tmp = it.next().toString(); 
  86.                                 sb = sb.append(tmp + ","); 
  87.                         } 
  88.                 } 
  89.                 String str = new String(sb.toString()); 
  90.                 str = str.substring(0, str.length() - 1); 
  91.                 return str; 
  92.         } 
  93.         /** 
  94.          * 获取某个单元格 
  95.          * @param row 
  96.          * @param col 
  97.          * @return 
  98.          */ 
  99.         public String getString(int row, int col) { 
  100.                 String temp = null
  101.                 int colnum = this.getColNum(); 
  102.                 if (colnum > 1) { 
  103.                         temp = list.get(row).toString().split(",")[col]; 
  104.                 } else if(colnum == 1){ 
  105.                         temp = list.get(row).toString(); 
  106.                 } else { 
  107.                         temp = null
  108.                 } 
  109.                 return temp; 
  110.         } 
  111.           
  112.         public void CsvClose()throws Exception{ 
  113.                 this.br.close(); 
  114.         } 
  115.         public static void main(String[] args)throws Exception { 
  116.                 CsvUtil util = new CsvUtil("D:\\demo.csv"); 
  117.                 int rowNum = util.getRowNum(); 
  118.                 int colNum = util.getColNum(); 
  119.                 String x = util.getRow(2); 
  120.                 String y = util.getCol(2); 
  121.                 System.out.println("rowNum:" + rowNum); 
  122.                 System.out.println("colNum:" + colNum); 
  123.                 System.out.println("x:" + x); 
  124.                 System.out.println("y:" + y); 
  125.                   
  126.                 for(int i=1;i<rowNum;i++){ 
  127.                         for(int j=0;j<colNum;j++){ 
  128.                                 System.out.println("result[" + i + "|" + j + "]:" + util.getString(i, j)); 
  129.                         } 
  130.                 } 
  131.                   
  132.         } 

 

你可能感兴趣的:(java,cvs)