Class CharSink
java.lang.Object
cloud.opencode.base.io.source.CharSink
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic CharSinknullSink()Creates a CharSink that discards all written data.Opens a new BufferedWriter for writing.abstract WriterOpens a new Writer for writing.static CharSinkCreates a CharSink for writing to a file path with UTF-8 encoding.static CharSinkCreates a CharSink for writing to a file path.static CharSinktoPathAppend(Path path) Creates a CharSink for appending to a file path with UTF-8 encoding.static CharSinktoPathAppend(Path path, Charset charset) Creates a CharSink for appending to a file path.voidwrite(CharSequence charSequence) Writes a character sequence.longWrites all characters from a Reader.voidwriteLines(Iterable<? extends CharSequence> lines) Writes lines with the system line separator.voidwriteLines(Iterable<? extends CharSequence> lines, String lineSeparator) Writes lines with a custom line separator.voidwriteLines(Stream<? extends CharSequence> lines) Writes lines from a stream with the system line separator.voidwriteLines(Stream<? extends CharSequence> lines, String lineSeparator) Writes lines from a stream with a custom line separator.
-
Constructor Details
-
CharSink
public CharSink()
-
-
Method Details
-
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
Opens a new BufferedWriter for writing. 打开新的BufferedWriter进行写入。- Returns:
- a new BufferedWriter | 新的BufferedWriter
- Throws:
OpenIOOperationException- if the writer cannot be opened | 如果Writer无法打开
-
write
Writes a character sequence. 写入字符序列。- Parameters:
charSequence- the character sequence to write | 要写入的字符序列- Throws:
OpenIOOperationException- if writing fails | 如果写入失败
-
writeFrom
Writes all characters from a Reader. 从Reader写入所有字符。- Parameters:
reader- the reader | Reader- Returns:
- the number of characters written | 写入的字符数
- Throws:
OpenIOOperationException- if writing fails | 如果写入失败
-
writeLines
Writes lines with the system line separator. 使用系统行分隔符写入多行。- Parameters:
lines- the lines to write | 要写入的行- Throws:
OpenIOOperationException- if writing fails | 如果写入失败
-
writeLines
Writes lines with a custom line separator. 使用自定义行分隔符写入多行。- Parameters:
lines- the lines to write | 要写入的行lineSeparator- the line separator | 行分隔符- Throws:
OpenIOOperationException- if writing fails | 如果写入失败
-
writeLines
Writes lines from a stream with the system line separator. 使用系统行分隔符从流写入多行。- Parameters:
lines- the line stream | 行流- Throws:
OpenIOOperationException- if writing fails | 如果写入失败
-
writeLines
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
-
toPath
-
toPathAppend
-
toPathAppend
-
nullSink
Creates a CharSink that discards all written data. 创建丢弃所有写入数据的CharSink。- Returns:
- a null CharSink | 空CharSink
-