package cn.unicom.util;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @description: 读写csv文件工具类
*
**/
public class CsvUtil {
public static void main(String[] args) throws Exception{
File file = new File("C:\\Users\\wcy\\Desktop\\His_20211013150000.csv");
InputStream inputStream = new FileInputStream(file);
List<String[]> list = read(inputStream,"GB2312");
write(list,"C:\\Users\\wcy\\Desktop\\His_202110131500001.csv");
}
/**
* 读取csv文件内容
* @param inputStream
* @param code csv文件的编码,如utf8,,gbk
*
* @return 返回csv文件中的数据
* @throws Exception
*/
public static List<String[]> read(InputStream inputStream, String code) throws Exception{
//1. 存储csv文件中的内容
List<String[]> csvList = new ArrayList<String[]>();
//2. 创建CsvReader
CsvReader reader = new CsvReader(inputStream, ',', Charset.forName(code));
//3. 跳过表头,如果需要表头的话,不要写这句
// reader.readHeaders();
//4.逐行读入除表头的数据
while(reader.readRecord()){
csvList.add(reader.getValues());
}
//5. 释放资源
reader.close();
return csvList;
}
/**
* 数据写入csv文件
* @param list UTF-8编码写入csv文件的内容
* @param filePath 写入的csv文件的指定路劲
*
* @throws Exception
*/
public static void write(List<String[]> list, String filePath) throws Exception{
CsvWriter wr = new CsvWriter(filePath, ',', Charset.forName("UTF-8"));
for (int i = 0; i < list.size(); i++) {
wr.writeRecord(list.get(i));
}
wr.close();
}
}
10-28
1318
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-22
569
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-04
03-19
1199
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-12
373
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)