CSVFileUtil 读取写入CSV文件简单工具类
/**
* @description CSV文件读取和输出 工具类.<br/>
* @author michael
* @date 2019/05/16
* @version Copyright (c) 2019, michael.xie@adsnova.cn All Rights Reserved.
*/
public class CSVFileUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(CSVFileUtil.class);
public static final String ENCODE = "UTF-8";
/**
* readCsv:根据路径读取CSV文件.<br/>
*
* @param csvFilePath
* @return
*/
public static List<String[]> readCsv(String csvFilePath) {
List<String[]> csvList = new ArrayList<String[]>();
try {
CsvReader reader = new CsvReader(csvFilePath, ',', Charset.forName(ENCODE));
/** 跳过表头 如果需要表头的话,不要写这句 */
reader.readHeaders();
while (reader.readRecord()) {
csvList.add(reader.getValues());
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
LOGGER.error("read csv file error. : {}", e.getLocalizedMessage());
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("read csv file error. : {}", e.getLocalizedMessage());
}
return csvList;
}
/**
* readCsv:根据数据流读取CSV文件.<br/>
*
* @param csvIs
* @return
*/
public static List<String[]> readCsv(InputStream csvIs) {
List<String[]> csvList = new ArrayList<String[]>();
try {
CsvReader reader = new CsvReader(csvIs, Charset.forName(ENCODE));
/** 跳过表头 如果需要表头的话,不需要写这句 */
reader.readHeaders();
while (reader.readRecord()) {
csvList.add(reader.getValues());
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
LOGGER.error("read csv inputStream error. : {}", e.getLocalizedMessage());
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("read csv inputStream error. : {}", e.getLocalizedMessage());
}
return csvList;
}
/**
* writeCsv:把内容写入到文件中.<br/>
*
* @param csvFilePath
* @param contents
*/
public static void writeCsv(String csvFilePath, List<String[]> contents) {
try {
CsvWriter wr = new CsvWriter(csvFilePath, ',', Charset.forName(ENCODE));
for (int i = 0; i < contents.size(); i++) {
wr.writeRecord(contents.get(i));
}
wr.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("write csv file error. : {}", e.getLocalizedMessage());
}
}
/**
*
* writeCsv:把内容写到输出流.<br/>
*
* @param ou
* @param list
*/
public static void writeCsv(OutputStream ou, List<String[]> list) {
CsvWriter cw = null;
try {
cw = new CsvWriter(ou, ',', Charset.forName(ENCODE));
for (String[] s : list) {
cw.writeRecord(s);
}
// 在文件中增加BOM,该处的byte[] 可以针对不同编码进行修改
ou.write(new byte[] {(byte)0xEF, (byte)0xBB, (byte)0xBF});
cw.flush();
} catch (IOException e) {
e.printStackTrace();
LOGGER.error("write csv outputStream error. : {}", e.getLocalizedMessage());
} finally {
if (null != cw) {
cw.close();
}
}
}
}
欢迎一起来学习和指导,谢谢关注!
本文来自博客园,作者:xiexie0812,转载请注明原文链接:https://www.cnblogs.com/mask-xiexie/p/10880564.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了