Class CsvWriter

java.lang.Object
cloud.opencode.base.csv.stream.CsvWriter
All Implemented Interfaces:
AutoCloseable

public final class CsvWriter extends Object implements AutoCloseable
Streaming CSV Writer - Incremental Row-by-Row Writing 流式 CSV 写入器 - 逐行增量写入

Writes CSV data incrementally to a Writer, formatting each row as it is written. Supports fluent API for convenient chaining.

增量地将 CSV 数据写入 Writer,在写入时格式化每一行。 支持流畅 API 以便于链式调用。

Usage | 使用方式:

try (CsvWriter writer = CsvWriter.of(path, CsvConfig.DEFAULT)) {
    writer.writeHeader("name", "age", "city")
          .writeRow("Alice", "30", "Beijing")
          .writeRow("Bob", "25", "Shanghai");
}

Security | 安全性:

  • Thread-safe: No (single-threaded sequential access) - 线程安全: 否(单线程顺序访问)
  • Applies formula injection protection if configured - 如果配置了则应用公式注入防护
Since:
JDK 25, opencode-base-csv V1.0.3
Author:
Leon Soo
  • Constructor Details

    • CsvWriter

      public CsvWriter(Writer writer, CsvConfig config)
      Constructs a CsvWriter from a Writer and configuration. 使用 Writer 和配置构造 CsvWriter。
      Parameters:
      writer - the output writer - 输出写入器
      config - the CSV configuration - CSV 配置
  • Method Details

    • of

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

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

      public static CsvWriter of(Writer writer, CsvConfig config)
      Creates a CsvWriter from a Writer. 从 Writer 创建 CsvWriter。
      Parameters:
      writer - the output writer - 输出写入器
      config - the CSV configuration - CSV 配置
      Returns:
      a new CsvWriter - 新的 CsvWriter
    • writeHeader

      public CsvWriter writeHeader(String... headers)
      Writes a header row. 写入标题行。
      Parameters:
      headers - the header field names - 标题字段名
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • writeHeader

      public CsvWriter writeHeader(List<String> headers)
      Writes a header row. 写入标题行。
      Parameters:
      headers - the header field names - 标题字段名列表
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • writeRow

      public CsvWriter writeRow(String... fields)
      Writes a data row from varargs. 从可变参数写入数据行。
      Parameters:
      fields - the field values - 字段值
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • writeRow

      public CsvWriter writeRow(CsvRow row)
      Writes a data row from a CsvRow. 从 CsvRow 写入数据行。
      Parameters:
      row - the row to write - 要写入的行
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • writeRow

      public CsvWriter writeRow(List<String> fields)
      Writes a data row from a list of field values. 从字段值列表写入数据行。
      Parameters:
      fields - the field values - 字段值列表
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • writeDocument

      public CsvWriter writeDocument(CsvDocument doc)
      Writes a complete CsvDocument (headers + all rows). 写入完整的 CsvDocument(标题 + 所有行)。
      Parameters:
      doc - the document to write - 要写入的文档
      Returns:
      this writer for fluent chaining - 此写入器,用于流畅链式调用
      Throws:
      CsvWriteException - if writing fails - 如果写入失败
    • flush

      public void flush()
      Flushes the underlying writer. 刷新底层写入器。
      Throws:
      CsvWriteException - if flushing fails - 如果刷新失败
    • close

      public void close()
      Flushes and closes the underlying writer. 刷新并关闭底层写入器。
      Specified by:
      close in interface AutoCloseable