Class CsvReader
java.lang.Object
cloud.opencode.base.csv.stream.CsvReader
- All Implemented Interfaces:
AutoCloseable, Iterable<CsvRow>
Streaming CSV Reader - Lazy Row-by-Row Parsing
流式 CSV 读取器 - 逐行懒加载解析
Reads CSV data incrementally from a Reader, producing one CsvRow
at a time. This avoids loading the entire document into memory, making it
suitable for processing large CSV files.
从 Reader 增量读取 CSV 数据,每次产生一个 CsvRow。
这避免了将整个文档加载到内存中,适合处理大型 CSV 文件。
Usage | 使用方式:
try (CsvReader reader = CsvReader.of(path, CsvConfig.DEFAULT)) {
reader.stream()
.filter(row -> !row.fields().get(0).isEmpty())
.forEach(System.out::println);
}
Security | 安全性:
- Thread-safe: No (single-threaded sequential access) - 线程安全: 否(单线程顺序访问)
- Enforces maxRows, maxColumns, maxFieldSize limits - 强制执行行数、列数、字段大小限制
- Since:
- JDK 25, opencode-base-csv V1.0.3
- Author:
- Leon Soo
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this reader and the underlying Reader.headers()Returns the header row.iterator()Returns an Iterator over CsvRows.static CsvReaderof(InputStream input, CsvConfig config) Creates a CsvReader from an InputStream.static CsvReaderCreates a CsvReader from a Reader.static CsvReaderCreates a CsvReader from a file Path.readAll()Reads all remaining rows and returns a complete CsvDocument.stream()Returns a Stream of CsvRows for functional-style processing.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Iterable
forEach, spliterator
-
Constructor Details
-
CsvReader
-
-
Method Details
-
of
Creates a CsvReader from an InputStream. 从 InputStream 创建 CsvReader。- Parameters:
input- the input stream - 输入流config- the CSV configuration - CSV 配置- Returns:
- a new CsvReader - 新的 CsvReader
-
of
-
of
-
headers
Returns the header row. Reads it on first call if the config has hasHeader=true. 返回标题行。如果配置了 hasHeader=true,则在首次调用时读取。- Returns:
- the header list, or an empty list if no headers - 标题列表,无标题时返回空列表
- Throws:
CsvParseException- if reading fails - 如果读取失败
-
iterator
Returns an Iterator over CsvRows. Each call returns the same logical iteration (the reader is stateful and can only be iterated once). 返回 CsvRow 的迭代器。每次调用返回相同的逻辑迭代 (读取器是有状态的,只能迭代一次)。 -
stream
-
readAll
Reads all remaining rows and returns a complete CsvDocument. 读取所有剩余行并返回完整的 CsvDocument。- Returns:
- the complete document - 完整文档
- Throws:
CsvParseException- if reading fails - 如果读取失败
-
close
public void close()Closes this reader and the underlying Reader. 关闭此读取器和底层 Reader。- Specified by:
closein interfaceAutoCloseable
-