Java环境下使用CsvReader()读取CSV文件

简介: Java环境下使用CsvReader()读取CSV文件

🐓CSV文件是什么

CSV 代表逗号分隔值,是一种非常流行的文件类型。CSV文件用于存储由逗号分隔的信息。文件的每一行都用于表示一个数据记录。

🐓CSV文件的读取

🚩进行CSV文件的解析

  private List<CsvRow> getCsvFile(String filePath) {
        //新建一个CsvReadConfig配置对象
        CsvReadConfig csvReadConfig = new CsvReadConfig();
        //新建一个CsvReader对象
        CsvReader csvRows = new CsvReader(csvReadConfig);
        //初始化数据
        CsvData read = null;
            read = csvRows.read(new File(filePath), CharsetUtil.CHARSET_UTF_8);
        //读取数据
        List<CsvRow> rows = read.getRows();
        return rows;
    }


🚩Hutool包中的CsvReadConfig配置类(可以动态设置开始和结束)

import java.io.Serializable;
 
public class CsvReadConfig extends CsvConfig<CsvReadConfig> implements Serializable {
    private static final long serialVersionUID = 5396453565371560052L;
    protected long headerLineNo = -1L;
    protected boolean skipEmptyRows = true;
    protected boolean errorOnDifferentFieldCount;
    protected long beginLineNo;
    protected long endLineNo = 9223372036854775806L;
    protected boolean trimField;
 
    public CsvReadConfig() {
    }
 
    public static CsvReadConfig defaultConfig() {
        return new CsvReadConfig();
    }
 
    public CsvReadConfig setContainsHeader(boolean containsHeader) {
        return this.setHeaderLineNo(containsHeader ? this.beginLineNo : -1L);
    }
 
    public CsvReadConfig setHeaderLineNo(long headerLineNo) {
        this.headerLineNo = headerLineNo;
        return this;
    }
 
    public CsvReadConfig setSkipEmptyRows(boolean skipEmptyRows) {
        this.skipEmptyRows = skipEmptyRows;
        return this;
    }
 
    public CsvReadConfig setErrorOnDifferentFieldCount(boolean errorOnDifferentFieldCount) {
        this.errorOnDifferentFieldCount = errorOnDifferentFieldCount;
        return this;
    }
 
    public CsvReadConfig setBeginLineNo(long beginLineNo) {
        this.beginLineNo = beginLineNo;
        return this;
    }
 
    public CsvReadConfig setEndLineNo(long endLineNo) {
        this.endLineNo = endLineNo;
        return this;
    }
 
    public CsvReadConfig setTrimField(boolean trimField) {
        this.trimField = trimField;
        return this;
    }
}


相关文章
|
SQL 存储 数据库
SQL实践篇(二):为什么微信用SQLite存储聊天记录
SQL实践篇(二):为什么微信用SQLite存储聊天记录
847 1
|
消息中间件 Apache RocketMQ
rocketmq客户端发送消息报错和超时问题
org.apache.rocketmq.remoting.exception.RemotingTimeoutException: wait response on the channel <10.0.21.69:10911> timeout, 1000(ms)、 closeChannel: close the connection to remote address
4836 1
rocketmq客户端发送消息报错和超时问题
|
存储 Java
SpringBoot导入和导出Csv文件(二十八)上
SpringBoot导入和导出Csv文件(二十八)上
1783 1
SpringBoot导入和导出Csv文件(二十八)上
|
设计模式 安全 Java
阿里开发手册 嵩山版-编程规约 (一)命名规范
该文章主要介绍了阿里开发手册嵩山版中关于编程规约的命名规范,包括代码命名的强制和推荐规定,以及接口、类、枚举等的命名规则和各层命名规约等内容。
 阿里开发手册 嵩山版-编程规约 (一)命名规范
|
机器学习/深度学习 消息中间件 算法
Flink ML的新特性解析与应用
本文整理自阿里巴巴算法专家赵伟波,在 Flink Forward Asia 2023 AI特征工程专场的分享。
130151 5
Flink ML的新特性解析与应用
|
存储 消息中间件 Java
深入理解Spring的TransactionSynchronizationManager
深入理解Spring的TransactionSynchronizationManager
1766 0
|
存储 JSON fastjson
聊聊fastjson反序列化的那些坑
聊聊fastjson反序列化的那些坑
3497 0
聊聊fastjson反序列化的那些坑
|
SQL 关系型数据库 MySQL
java.sql.SQLException: No operations allowed after statement closed.
java.sql.SQLException: No operations allowed after statement closed.
658 0
|
设计模式 JSON Dubbo
超越接口:探索Dubbo的泛化调用机制
超越接口:探索Dubbo的泛化调用机制
1367 0

热门文章

最新文章