当前位置: 移动技术网 > IT编程>开发语言>Java > java读取csv文件内容示例代码

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

2019年07月22日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下: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));
                        }
                }

        }
}

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网