Class CharSink

java.lang.Object
cloud.opencode.base.io.source.CharSink

public abstract class CharSink extends Object
Abstract Character Sink - A writable destination for characters 抽象字符接收器 - 可写的字符目标

An abstraction over different destinations for character data. Counterpart to CharSource.

对不同字符数据目标的抽象。是CharSource的对应物。

Features | 主要功能:

  • Unified character writing - 统一的字符写入
  • Write from Reader - 从Reader写入
  • Write strings and character sequences - 写入字符串和字符序列
  • Write lines - 写入多行
  • Support for append mode - 支持追加模式

Usage Examples | 使用示例:

// Write to file
CharSink sink = CharSink.toPath(Paths.get("output.txt"));
sink.write("Hello, World!");

// Write lines
sink.writeLines(List.of("Line 1", "Line 2", "Line 3"));

// Copy from source to sink
CharSource source = CharSource.fromPath(Paths.get("input.txt"));
source.copyTo(sink);

Security | 安全性:

  • Thread-safe: No, not safe for concurrent writes - 线程安全: 否,不支持并发写入
  • Null-safe: No, data must not be null - 空值安全: 否,数据不可为null
Since:
JDK 25, opencode-base-io V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • CharSink

      public CharSink()
  • Method Details

    • openStream

      public abstract Writer openStream()
      Opens a new Writer for writing. 打开新的Writer进行写入。

      The caller is responsible for closing the writer.

      调用者负责关闭Writer。

      Returns:
      a new Writer | 新的Writer
      Throws:
      OpenIOOperationException - if the writer cannot be opened | 如果Writer无法打开
    • openBufferedStream

      public BufferedWriter openBufferedStream()
      Opens a new BufferedWriter for writing. 打开新的BufferedWriter进行写入。
      Returns:
      a new BufferedWriter | 新的BufferedWriter
      Throws:
      OpenIOOperationException - if the writer cannot be opened | 如果Writer无法打开
    • write

      public void write(CharSequence charSequence)
      Writes a character sequence. 写入字符序列。
      Parameters:
      charSequence - the character sequence to write | 要写入的字符序列
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeFrom

      public long writeFrom(Reader reader)
      Writes all characters from a Reader. 从Reader写入所有字符。
      Parameters:
      reader - the reader | Reader
      Returns:
      the number of characters written | 写入的字符数
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeLines

      public void writeLines(Iterable<? extends CharSequence> lines)
      Writes lines with the system line separator. 使用系统行分隔符写入多行。
      Parameters:
      lines - the lines to write | 要写入的行
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeLines

      public void writeLines(Iterable<? extends CharSequence> lines, String lineSeparator)
      Writes lines with a custom line separator. 使用自定义行分隔符写入多行。
      Parameters:
      lines - the lines to write | 要写入的行
      lineSeparator - the line separator | 行分隔符
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeLines

      public void writeLines(Stream<? extends CharSequence> lines)
      Writes lines from a stream with the system line separator. 使用系统行分隔符从流写入多行。
      Parameters:
      lines - the line stream | 行流
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • writeLines

      public void writeLines(Stream<? extends CharSequence> lines, String lineSeparator)
      Writes lines from a stream with a custom line separator. 使用自定义行分隔符从流写入多行。
      Parameters:
      lines - the line stream | 行流
      lineSeparator - the line separator | 行分隔符
      Throws:
      OpenIOOperationException - if writing fails | 如果写入失败
    • toPath

      public static CharSink toPath(Path path)
      Creates a CharSink for writing to a file path with UTF-8 encoding. 创建用于写入文件路径的使用UTF-8编码的CharSink。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a CharSink | CharSink
    • toPath

      public static CharSink toPath(Path path, Charset charset)
      Creates a CharSink for writing to a file path. 创建用于写入文件路径的CharSink。
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      a CharSink | CharSink
    • toPathAppend

      public static CharSink toPathAppend(Path path)
      Creates a CharSink for appending to a file path with UTF-8 encoding. 创建用于追加到文件路径的使用UTF-8编码的CharSink。
      Parameters:
      path - the file path | 文件路径
      Returns:
      a CharSink | CharSink
    • toPathAppend

      public static CharSink toPathAppend(Path path, Charset charset)
      Creates a CharSink for appending to a file path. 创建用于追加到文件路径的CharSink。
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      a CharSink | CharSink
    • nullSink

      public static CharSink nullSink()
      Creates a CharSink that discards all written data. 创建丢弃所有写入数据的CharSink。
      Returns:
      a null CharSink | 空CharSink