Class FileWriter

java.lang.Object
cloud.opencode.base.io.file.FileWriter

public final class FileWriter extends Object
File Writer Utility 文件写入器

Fluent API for writing files with various options. Provides convenient methods for different writing scenarios.

用于写入文件的流式API。 提供适用于不同写入场景的便捷方法。

Features | 主要功能:

  • Fluent API - 流式API
  • Charset configuration - 字符集配置
  • Append mode support - 追加模式支持
  • Create parent directories - 创建父目录

Usage Examples | 使用示例:

// Write string
FileWriter.of(path).write("Hello World");

// Append content
FileWriter.of(path).append().write("More content");

// Write with charset
FileWriter.of(path).charset(StandardCharsets.ISO_8859_1).write(content);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • of

      public static FileWriter of(Path path)
      Creates a file writer for the given path 为给定路径创建文件写入器
      Parameters:
      path - the file path | 文件路径
      Returns:
      file writer | 文件写入器
    • of

      public static FileWriter of(String path)
      Creates a file writer for the given path string 为给定路径字符串创建文件写入器
      Parameters:
      path - the file path string | 文件路径字符串
      Returns:
      file writer | 文件写入器
    • charset

      public FileWriter charset(Charset charset)
      Sets the charset 设置字符集
      Parameters:
      charset - the charset | 字符集
      Returns:
      this | 当前对象
    • append

      public FileWriter append()
      Enables append mode 启用追加模式
      Returns:
      this | 当前对象
    • append

      public FileWriter append(boolean append)
      Sets append mode 设置追加模式
      Parameters:
      append - whether to append | 是否追加
      Returns:
      this | 当前对象
    • noCreateParents

      public FileWriter noCreateParents()
      Disables automatic parent directory creation 禁用自动创建父目录
      Returns:
      this | 当前对象
    • write

      public void write(byte[] bytes)
      Writes byte array to file 写入字节数组到文件
      Parameters:
      bytes - the content | 内容
    • write

      public void write(CharSequence content)
      Writes string to file 写入字符串到文件
      Parameters:
      content - the content | 内容
    • writeLines

      public void writeLines(Iterable<? extends CharSequence> lines)
      Writes lines to file 写入行到文件
      Parameters:
      lines - the lines | 行
    • asBufferedWriter

      public BufferedWriter asBufferedWriter()
      Gets a buffered writer 获取缓冲写入器
      Returns:
      buffered writer | 缓冲写入器
    • asOutputStream

      public OutputStream asOutputStream()
      Gets an output stream 获取输出流
      Returns:
      output stream | 输出流
    • getPath

      public Path getPath()
      Gets the path 获取路径
      Returns:
      path | 路径