程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java讀取csv文件內容示例代碼

java讀取csv文件內容示例代碼

編輯:關於JAVA

java讀取csv文件內容示例代碼。本站提示廣大學習愛好者:(java讀取csv文件內容示例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是java讀取csv文件內容示例代碼正文



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

        }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved