Java通过javacsv实现读取csv文件数据
目录
1、添加依赖
<!--csv文件操作-->
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
2、测试的csv文件
3、实际调用代码
package com.shucha.deveiface.biz.test;
import com.csvreader.CsvReader;
import com.monitorjbl.xlsx.StreamingReader;
import com.sdy.common.model.BizException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.xmlbeans.impl.xb.xsdschema.All;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2022-05-07 11:29
*/
public class test2022 {
public static void main(String[] args) throws BizException, IOException {
File csvFile = new File("D:\\123\\测试csv.csv");
readCsvByCsvReader(csvFile);
readCsvByBufferedReader(csvFile.getPath());
}
public static Map<String, Object> readCsvByCsvReader(File file) {
Map<String, Object> mapData = new HashMap<>();
String fileName = file.getName();
fileName = fileName.substring(0, fileName.lastIndexOf("."));
mapData.put("sheetName",fileName);
ArrayList<String> strList = new ArrayList<>();
List<Map<String, Object>> list = new ArrayList<>();
try {
ArrayList<String[]> arrList = new ArrayList<String[]>();
// 如果生产文件乱码,windows下用gbk,linux用UTF-8
CsvReader reader = new CsvReader(file.getPath(), ',', Charset.forName("UTF-8"));
// 读取表头
reader.readHeaders();
String[] headArray = reader.getHeaders();//获取标题
// System.out.println(headArray);
while (reader.readRecord()) {
// System.out.println(Arrays.asList(reader.getValues()));
// 按行读取,并把每一行的数据添加到list集合
arrList.add(reader.getValues());
}
reader.close();
// System.out.println("读取的行数:" + arrList.size());
// 如果要返回 String[] 类型的 list 集合,则直接返回 arrList
// 以下步骤是把 String[] 类型的 list 集合转化为 String 类型的 list 集合
for (int i = 1; i < arrList.size(); i++) {
// 组装String字符串
// 如果不知道有多少列,则可再加一个循环
Map<String, Object> map = new HashMap<>();
for (int j=0;j<arrList.get(0).length;j++) {
map.put(""+arrList.get(0)[j]+"", arrList.get(i)[j]);
}
// 返回的数格为拼接
/*String ele = arrList.get(i)[0] + "," + arrList.get(i)[1] + "," + arrList.get(i)[2];
System.out.println(ele);
strList.add(ele);*/
list.add(map);
}
} catch (Exception e) {
e.printStackTrace();
}
mapData.put("data",list);
return mapData;
}
/**
* BufferedReader 读取
* @param filePath
* @return
*/
public static ArrayList<String> readCsvByBufferedReader(String filePath) {
File csv = new File(filePath);
csv.setReadable(true);
csv.setWritable(true);
InputStreamReader isr = null;
BufferedReader br = null;
try {
isr = new InputStreamReader(new FileInputStream(csv), "UTF-8");
br = new BufferedReader(isr);
} catch (Exception e) {
e.printStackTrace();
}
String line = "";
ArrayList<String> records = new ArrayList<>();
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
records.add(line);
}
System.out.println("csv表格读取行数:" + records.size());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(records);
return records;
}
}
4、返回的数据格式
第一个方法返回数据:
{sheetName=测试csv, data=[{name=第1条数据, id=1, type=语文}, {name=第2条数据, id=2, type=数学}, {name=第3条数据, id=3, type=英语}, {name=第4条数据, id=4, type=美术}, {name=第5条数据, id=5, type=体育}, {name=第6条数据, id=6, type=}]}
第二个方法返回数据:
["id","name","type", "1","第1条数据","语文", "2","第2条数据","数学", "3","第3条数据","英语", "4","第4条数据","美术", "5","第5条数据","体育", "6","第6条数据",]