Class CsvReader

java.lang.Object
cloud.opencode.base.csv.stream.CsvReader
All Implemented Interfaces:
AutoCloseable, Iterable<CsvRow>

public final class CsvReader extends Object implements Iterable<CsvRow>, AutoCloseable
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 Details

    • CsvReader

      public CsvReader(Reader reader, CsvConfig config)
      Constructs a CsvReader from a Reader and configuration. 使用 Reader 和配置构造 CsvReader。
      Parameters:
      reader - the input reader - 输入读取器
      config - the CSV configuration - CSV 配置
  • Method Details

    • of

      public static CsvReader of(InputStream input, CsvConfig config)
      Creates a CsvReader from an InputStream. 从 InputStream 创建 CsvReader。
      Parameters:
      input - the input stream - 输入流
      config - the CSV configuration - CSV 配置
      Returns:
      a new CsvReader - 新的 CsvReader
    • of

      public static CsvReader of(Path file, CsvConfig config)
      Creates a CsvReader from a file Path. 从文件路径创建 CsvReader。
      Parameters:
      file - the file path - 文件路径
      config - the CSV configuration - CSV 配置
      Returns:
      a new CsvReader - 新的 CsvReader
    • of

      public static CsvReader of(Reader reader, CsvConfig config)
      Creates a CsvReader from a Reader. 从 Reader 创建 CsvReader。
      Parameters:
      reader - the input reader - 输入读取器
      config - the CSV configuration - CSV 配置
      Returns:
      a new CsvReader - 新的 CsvReader
    • headers

      public List<String> 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

      public Iterator<CsvRow> iterator()
      Returns an Iterator over CsvRows. Each call returns the same logical iteration (the reader is stateful and can only be iterated once). 返回 CsvRow 的迭代器。每次调用返回相同的逻辑迭代 (读取器是有状态的,只能迭代一次)。
      Specified by:
      iterator in interface Iterable<CsvRow>
      Returns:
      a row iterator - 行迭代器
    • stream

      public Stream<CsvRow> stream()
      Returns a Stream of CsvRows for functional-style processing. 返回 CsvRow 的 Stream,用于函数式处理。
      Returns:
      a stream of rows - 行流
    • readAll

      public CsvDocument 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:
      close in interface AutoCloseable