// 读文件:3 行
EasyTxt.read(file, UserBean.class, ",", new BeanReadListener()).doRead();
// 写文件:3 行
EasyTxt.write(file, UserBean.class, ",", pageNo, pageSize, this::queryPage).doWrite();- 注解驱动 Bean 映射 —
@TxtFiled、@DateFormatFiled、@NumberFormatFiled - 双索引模式 — 固定索引 & 自动升序,灵活应对不同文本结构
- 分批读写 — 分页回调,避免大文件内存溢出
- 2G+ 大文件支持 — 实测 2.52GB 写入 111s,读取 105s(见下方性能表)
- 丰富类型转换 — String、int、long、BigDecimal、Date、LocalDate、LocalDateTime 等 15+ 类型
- 自定义分隔符 — CSV、管道符
|、Tab 或任意字符 - 零重量框架 — 无 Spring 依赖,纯 Java + Hutool + Commons IO
| 能力 | easy-txt | OpenCSV | Apache Commons CSV |
|---|---|---|---|
| 注解 Bean 映射 | ✅ | ❌ (需 MappingStrategy) | ❌ |
| 分批回调读写 | ✅ | ❌ | ❌ |
| 2G+ 大文件 | ✅ | ✅ | ✅ |
| 自定义分隔符 | ✅ | ✅ | ✅ |
| 内置类型转换器 | ✅ (15+ 种) | ❌ | |
| 日期/数字格式注解 | ✅ | ❌ | ❌ |
<dependency>
<groupId>com.small</groupId>
<artifactId>easy-txt-core</artifactId>
<version>1.1.0</version>
</dependency>implementation 'com.small:easy-txt-core:1.1.0'git clone https://github.com/sm-rose/easy-txt.git
cd easy-txt
mvn clean packageJAR 包生成在 easy-txt-core/target/easy-txt-core-1.1.0.jar。
@Data
public class UserBean {
@TxtFiled(index = 0)
private String name;
@TxtFiled(index = 1)
private int age;
@TxtFiled(index = 2)
private BigDecimal salary;
@TxtFiled(index = 3)
@DateFormatFiled("yyyy-MM-dd")
private Date birth;
}// 通过监听器逐行处理
EasyTxt.read(file, UserBean.class, ",", new BeanReadListener()).doRead();
// 通过 Consumer 分批消费
EasyTxt.read(file, BeanTestVO.class, "\\|", pageSize, pageList -> {
pageList.forEach(System.out::println);
}).doRead();EasyTxt.write(file, BeanTest.class, ",", pageNo, pageSize, (pNum, pSize) -> {
return queryPageData(pNum, pSize);
}).doWrite();完整示例见
easy-txt-test模块。
测试环境: Windows 10, 16G 内存, 机械硬盘(已使用 4 年)。
| 操作 | 数据行 | 文件大小 | 耗时 |
|---|---|---|---|
| 写入 | 2,000,000 | 2.52 GB | 111s |
| 读取 | 2,000,000 | 2.52 GB | 105s |
| 操作 | 数据行 | 文件大小 | 耗时 |
|---|---|---|---|
| 写入 | 8,000,000 | 2.21 GB | 186s |
| 读取 | 8,000,000 | 2.52 GB | 223s |
详见 BigFileDemoTest 和 BigFileTypeDemoTest。
