Java读取csv文件
2013-04-26 14:02
337 查看
要做个批量导入,但是要暂停了,先放这儿,别丢了
本文出自 “IT-菜菜” 博客,请务必保留此出处http://hanbin.blog.51cto.com/1518861/1187308
package com.huateng.readcsv; import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class CsvUtil { private String fileName = null; private BufferedReader br = null; private List<String> list = new ArrayList<String>(); public CsvUtil() { } public CsvUtil(String fileName) throws Exception { this.fileName = fileName; br = new BufferedReader(new FileReader(fileName)); String stemp; while ((stemp = br.readLine()) != null) { list.add(stemp); } } public List getList() { return list; } /** * 获取行数 * @return */ public int getRowNum() { return list.size(); } /** * 获取列数 * @return */ public int getColNum() { if (!list.toString().equals("[]")) { if (list.get(0).toString().contains(",")) {// csv为逗号分隔文件 return list.get(0).toString().split(",").length; } else if (list.get(0).toString().trim().length() != 0) { return 1; } else { return 0; } } else { return 0; } } /** * 获取制定行 * @param index * @return */ public String getRow(int index) { if (this.list.size() != 0) { return (String) list.get(index); } else { return null; } } /** * 获取指定列 * @param index * @return */ public String getCol(int index) { if (this.getColNum() == 0) { return null; } StringBuffer sb = new StringBuffer(); String tmp = null; int colnum = this.getColNum(); if (colnum > 1) { for (Iterator it = list.iterator(); it.hasNext();) { tmp = it.next().toString(); sb = sb.append(tmp.split(",")[index] + ","); } } else { for (Iterator it = list.iterator(); it.hasNext();) { tmp = it.next().toString(); sb = sb.append(tmp + ","); } } String str = new String(sb.toString()); str = str.substring(0, str.length() - 1); return str; } /** * 获取某个单元格 * @param row * @param col * @return */ public String getString(int row, int col) { String temp = null; int colnum = this.getColNum(); if (colnum > 1) { temp = list.get(row).toString().split(",")[col]; } else if(colnum == 1){ temp = list.get(row).toString(); } else { temp = null; } return temp; } public void CsvClose()throws Exception{ this.br.close(); } public static void main(String[] args)throws Exception { CsvUtil util = new CsvUtil("D:\\demo.csv"); int rowNum = util.getRowNum(); int colNum = util.getColNum(); String x = util.getRow(2); String y = util.getCol(2); System.out.println("rowNum:" + rowNum); System.out.println("colNum:" + colNum); System.out.println("x:" + x); System.out.println("y:" + y); for(int i=1;i<rowNum;i++){ for(int j=0;j<colNum;j++){ System.out.println("result[" + i + "|" + j + "]:" + util.getString(i, j)); } } } }
本文出自 “IT-菜菜” 博客,请务必保留此出处http://hanbin.blog.51cto.com/1518861/1187308
相关文章推荐
- java ibatis 读取mysql blob类型乱码 spring mvc文件下载csv文件乱码
- java读取目录下所有csv文件数据,存入三维数组并返回
- Java里面读取csv文件中文乱码
- [Jmeter]通过批处理调用java,java从CSV动态读取登录的用户名和密码,并将其作为参数组合成字符串,写入外部.bat文件,然后通过Java执行这个外部批处理文件
- Java从.CSV文件中读取数据和写入
- java的简单反射运用实例(读取csv文件映射为对象)
- java将数据写入csv文件,从csv文件中读取数据
- SparkR读取CSV格式文件错误java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.spark.u
- java 读取 写入 csv 文件
- Java读取CSV文件并导出
- Java 读取csv文件
- java读取csv文件并将其转成json
- Java读取CSV文件内容
- java读取导入csv格式 文件
- Java 读取CSV文件
- java使用CsvReader和CsvWriter对csv文件内容进行读取和写入操作
- Java读取和写入CSV文件
- Java读取csv文件并将内容插入到数据库
- java读取CSV文件
- Java读取csv文件