Class FileReader

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

public final class FileReader extends Object
File Reader Utility 文件读取器

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

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

Features | 主要功能:

  • Fluent API - 流式API
  • Charset configuration - 字符集配置
  • Multiple read modes - 多种读取模式

Usage Examples | 使用示例:

// Read as string
String content = FileReader.of(path).asString();

// Read lines
List<String> lines = FileReader.of(path).asLines();

// Read with charset
String content = FileReader.of(path).charset(StandardCharsets.ISO_8859_1).asString();

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 FileReader of(Path path)
      Creates a file reader for the given path 为给定路径创建文件读取器
      Parameters:
      path - the file path | 文件路径
      Returns:
      file reader | 文件读取器
    • of

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

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

      public byte[] asBytes()
      Reads file as byte array 读取文件为字节数组
      Returns:
      byte array | 字节数组
    • asString

      public String asString()
      Reads file as string 读取文件为字符串
      Returns:
      content string | 内容字符串
    • asLines

      public List<String> asLines()
      Reads file as list of lines 读取文件为行列表
      Returns:
      list of lines | 行列表
    • asLineStream

      public Stream<String> asLineStream()
      Reads file as stream of lines 读取文件为行流
      Returns:
      stream of lines | 行流
    • asBufferedReader

      public BufferedReader asBufferedReader()
      Gets a buffered reader 获取缓冲读取器
      Returns:
      buffered reader | 缓冲读取器
    • asInputStream

      public InputStream asInputStream()
      Gets an input stream 获取输入流
      Returns:
      input stream | 输入流
    • firstLine

      public String firstLine()
      Reads first line of file 读取文件首行
      Returns:
      first line or null | 首行或null
    • lastLine

      public String lastLine()
      Reads last line of file 读取文件末行
      Returns:
      last line or null | 末行或null
    • exists

      public boolean exists()
      Checks if file exists 检查文件是否存在
      Returns:
      true if exists | 如果存在返回true
    • size

      public long size()
      Gets file size 获取文件大小
      Returns:
      size in bytes | 字节大小
    • getPath

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