Class FileReader
java.lang.Object
cloud.opencode.base.io.file.FileReader
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 Summary
Modifier and TypeMethodDescriptionGets a buffered reader 获取缓冲读取器byte[]asBytes()Reads file as byte array 读取文件为字节数组Gets an input stream 获取输入流asLines()Reads file as list of lines 读取文件为行列表Reads file as stream of lines 读取文件为行流asString()Reads file as string 读取文件为字符串Sets the charset 设置字符集booleanexists()Checks if file exists 检查文件是否存在Reads first line of file 读取文件首行getPath()Gets the path 获取路径lastLine()Reads last line of file 读取文件末行static FileReaderCreates a file reader for the given path string 为给定路径字符串创建文件读取器static FileReaderCreates a file reader for the given path 为给定路径创建文件读取器longsize()Gets file size 获取文件大小
-
Method Details
-
of
Creates a file reader for the given path 为给定路径创建文件读取器- Parameters:
path- the file path | 文件路径- Returns:
- file reader | 文件读取器
-
of
Creates a file reader for the given path string 为给定路径字符串创建文件读取器- Parameters:
path- the file path string | 文件路径字符串- Returns:
- file reader | 文件读取器
-
charset
Sets the charset 设置字符集- Parameters:
charset- the charset | 字符集- Returns:
- this | 当前对象
-
asBytes
public byte[] asBytes()Reads file as byte array 读取文件为字节数组- Returns:
- byte array | 字节数组
-
asString
-
asLines
-
asLineStream
-
asBufferedReader
Gets a buffered reader 获取缓冲读取器- Returns:
- buffered reader | 缓冲读取器
-
asInputStream
-
firstLine
-
lastLine
-
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
-