java csv 转json_java读取csv文件并将其转成json

该博客介绍了一个Java工具类`CsvUtil`,用于读取CSV文件并将其内容转化为List。类中包含读取CSV文件、获取行数、列数、指定行、指定列和单元格的方法,并提供了去除表头的功能,方便将CSV数据转换成JSON格式。
摘要由CSDN通过智能技术生成

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 String removehead(String str){

String[] str_1=str.split(",");

String sb=new String();

for(int i=1;i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值