csv文件操作工具類

package com.ksoft.controller.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ReadeCsvUtil {

private BufferedReader br = null;
private List<String> list = new ArrayList<String>();

public ReadeCsvUtil() {

}

/**
 * 創建一個csv文件的工具類
 * @param filePath 文件路徑
 * @throws Exception
 */
public ReadeCsvUtil(String filePath) throws Exception {
    br = new BufferedReader(new FileReader(filePath));
    String stemp;
    while ((stemp = br.readLine()) != null) {
        list.add(stemp);
    }
    this.CsvClose();
}
/**
 * 創建一個csv文件的工具類
 * @param inputStream 數據流
 * @throws Exception
 */
public ReadeCsvUtil(InputStream inputStream)throws Exception{
    br = new BufferedReader(new InputStreamReader(inputStream));
    String stemp;
    while ((stemp = br.readLine()) != null) {
        list.add(stemp);
    }
    inputStream.close();
    this.CsvClose();
}

public List<String> 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<String> it = list.iterator(); it.hasNext();) {
            tmp = it.next().toString();
            sb = sb.append(tmp.split(",")[index] + ",");
        }
    } else {
        for (Iterator<String> 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;
}

private void CsvClose() throws Exception {
    this.br.close();
}

public static void main(String[] args) throws Exception {
    ReadeCsvUtil util = new ReadeCsvUtil("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));
        }
    }

}

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章