java批量读取csv文件_Java读取csv文件

java批量读取csv文件_Java读取csv文件packagecom huateng readcsv importjava io BufferedRead importjava io FileReader importjava util ArrayList importjava util Iterator importjava util List publicclassC privateStrin

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 list = new ArrayList();

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

for(int j=0;j

System.out.println(“result[” + i + “|” + j + “]:” + util.getString(i, j));

}

}

}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/200489.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月20日 上午10:43
下一篇 2026年3月20日 上午10:44


相关推荐

  • 如何修改opkg 源

    如何修改opkg 源http://downloads.openwrt.org.cn/上面的链接是openwrt国内的源,但是只有适合以下几个系统的源Hacked/18-Jun-201413:58-OpenWrt-DreamBox/01-J

    2022年5月9日
    213
  • 排序方法

    排序方法

    2021年8月12日
    58
  • flashfxp中文激活成功教程版|flashfxp v5.4绿色激活成功教程版下载免注册码(强大的fxp/ftp客户端)

    flashfxp中文激活成功教程版|flashfxp v5.4绿色激活成功教程版下载免注册码(强大的fxp/ftp客户端)flashfxp中文激活成功教程版算是一款比较老牌FTP/FXP传输必备软件,其功能的强大性想必你早有耳闻?它具备最常用的FTP,FXP,FTPS,SFTP上传下载功能,不仅支持上传和下载站点的文档,图片,视频,音乐等文件,还支持文件夹(带子文件夹)的文件传送、删除和FTP代理及Socks3&4,同样也支持转移和备份本地和远程的文件,以及FXP服务器之间的传输和转移。支持多语言,包括简体中文在内。

    2022年7月26日
    45
  • Matlab 直方图_matlab分析

    Matlab 直方图_matlab分析绘制直方图(水平和垂直的)语法bar(Y)bar(x,Y)bar(…,width)bar(…,’style’)bar(…,’bar_color’)bar(axes_handle,…)h=bar(…)hpatches=bar(‘v6’,…)barh(…)h=barh(…)hpatches=barh(‘v6

    2022年10月18日
    4
  • 关于敏捷软件开发

    关于敏捷软件开发最近在看 对其中的原则和实践非常感兴趣 顺便记录下来 1 敏捷软件开发宣言 nbsp 个体和交互 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 过程和工具 nbsp 可以工作的软件 nbsp 胜过 nbsp nbsp 面面俱到的文档 nbsp 客户合作 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 合同谈判 nbsp 响应变化 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 胜过 nbsp nbsp 遵循计划 2 12 个原则 nbsp 2 1 我们最优先要做的是通过尽早的 持续的交付有价值的软件来使客户满意 nbsp 2

    2026年3月17日
    2
  • 详述 AI 应用落地的三个阶段

    详述 AI 应用落地的三个阶段

    2026年3月16日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号