欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java读取csv文件内容示例代码

程序员文章站 2024-02-17 08:48:34
复制代码 代码如下:package com.huateng.readcsv;import java.io.bufferedreader;import java.io.fil...

复制代码 代码如下:

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

        }
}