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

时间:2023-03-13 22:09:14 


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

        }
}

标签:java读取csv文件内容
0
投稿

猜你喜欢

  • 深入理解java内置锁(synchronized)和显式锁(ReentrantLock)

    2023-11-19 00:10:57
  • Java利用Swagger2自动生成对外接口的文档

    2023-03-31 03:22:38
  • opencv 做人脸识别 opencv 人脸匹配分析

    2023-07-09 06:34:44
  • response文件流输出文件名中文不显示的解决

    2023-02-06 19:41:02
  • Spring源码解析之BeanPostProcessor知识总结

    2022-04-07 22:13:34
  • java根据网络地址保存图片的方法

    2021-09-01 18:37:02
  • java 汉诺塔Hanoi递归、非递归(仿系统递归)和非递归规律 实现代码

    2023-09-13 11:29:31
  • Java权重随机的实现方法

    2021-10-05 14:27:50
  • java迷宫算法的理解(递归分割,递归回溯,深搜,广搜)

    2022-10-22 10:36:31
  • Spring Boot 与 Kotlin 上传文件的示例代码

    2022-08-24 10:24:08
  • Maven安装及MyEclipse中使用Maven

    2023-06-20 04:29:07
  • MyBatis Log 插件无法显示SQL语句的原因解析

    2023-11-24 23:42:55
  • 带你了解Spring中bean的获取

    2021-10-10 09:53:10
  • C#实现简单俄罗斯方块

    2023-06-18 07:18:36
  • 深入剖析构建JSON字符串的三种方式(推荐)

    2023-09-26 07:47:22
  • android效果TapBarMenu绘制底部导航栏的使用方式示例

    2023-07-29 20:53:36
  • Java 中String StringBuilder 与 StringBuffer详解及用法实例

    2021-06-17 12:25:32
  • ElasticSearch添加索引代码实例解析

    2023-11-21 03:41:04
  • 总结Java集合类操作优化经验

    2023-01-27 10:29:37
  • 浅谈Java中复制数组的方式

    2022-04-14 23:30:27
  • asp之家 软件编程 m.aspxhome.com