Class OpenIO
java.lang.Object
cloud.opencode.base.io.OpenIO
File and Directory Operations Utility Class
文件和目录操作工具类
Primary facade for file and directory operations based on NIO.2 API. Provides convenient methods for common IO operations with unchecked exceptions.
基于NIO.2 API的文件和目录操作主门面类。 提供便捷的常用IO操作方法,使用非受检异常。
Features | 主要功能:
- File reading and writing - 文件读写
- File and directory operations - 文件和目录操作
- Directory traversal - 目录遍历
- Pattern matching (glob/regex) - 模式匹配
Usage Examples | 使用示例:
// Read file
String content = OpenIO.readString(path);
List<String> lines = OpenIO.readLines(path);
// Write file
OpenIO.writeString(path, "Hello World");
OpenIO.writeLines(path, List.of("line1", "line2"));
// File operations
OpenIO.copy(source, target);
OpenIO.deleteRecursively(dir);
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 TypeMethodDescriptionstatic voidappend(Path path, CharSequence content) Appends content to file 追加内容到文件static voidappendLines(Path path, Iterable<? extends CharSequence> lines) Appends lines to file 追加行到文件static Pathcopy(Path source, Path target, CopyOption... options) Copies a file 复制文件static voidcopyRecursively(Path source, Path target, CopyOption... options) Copies directory recursively 递归复制目录static PathcreateDirectories(Path path) Creates directories including parents 创建目录(包含父目录)static PathcreateDirectory(Path path) Creates a directory 创建目录static PathcreateFile(Path path) Creates a file 创建文件static voidDeletes a file or empty directory 删除文件或空目录static booleandeleteIfExists(Path path) Deletes file if exists 删除文件(如果存在)static voiddeleteRecursively(Path path) Deletes directory recursively 递归删除目录static longdirectorySize(Path path) Gets directory size recursively 递归获取目录大小static booleanexists(Path path, LinkOption... options) Checks if path exists 检查路径是否存在find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher) Finds files with custom matcher 使用自定义匹配器查找文件Finds files matching regex pattern 查找匹配正则表达式的文件static InstantgetLastModifiedTime(Path path) Gets last modified time 获取最后修改时间Finds files matching glob pattern 查找匹配glob模式的文件static booleanisDirectory(Path path) Checks if path is a directory 检查路径是否为目录static booleanisEmptyDirectory(Path path) Checks if directory is empty 检查目录是否为空static booleanChecks if path is a file 检查路径是否为文件static booleanChecks if path is hidden 检查路径是否隐藏static booleanisReadable(Path path) Checks if path is readable 检查路径是否可读static booleanisSameFile(Path path1, Path path2) Checks if two paths point to the same file 检查两个路径是否指向同一文件static booleanisSymbolicLink(Path path) Checks if path is a symbolic link 检查路径是否为符号链接static booleanisWritable(Path path) Checks if path is writable 检查路径是否可写Returns a stream of lines from file (UTF-8) 返回文件的行流(UTF-8)Returns a stream of lines from file 返回文件的行流Lists directory contents 列出目录内容static Pathmove(Path source, Path target, CopyOption... options) Moves a file 移动文件static byte[]Reads file as byte array 读取文件为字节数组static StringreadFirstLine(Path path) Reads the first line of file 读取文件首行Reads file as list of lines (UTF-8) 读取文件为行列表(UTF-8)Reads file as list of lines 读取文件为行列表static StringreadString(Path path) Reads file as UTF-8 string 读取文件为UTF-8字符串static StringreadString(Path path, Charset charset) Reads file as string 读取文件为字符串static voidsetLastModifiedTime(Path path, Instant time) Sets last modified time 设置最后修改时间static longGets file size 获取文件大小Walks directory tree with unlimited depth 遍历目录树(无深度限制)Walks directory tree 遍历目录树static voidwriteBytes(Path path, byte[] bytes, OpenOption... options) Writes byte array to file 写入字节数组到文件static voidwriteLines(Path path, Iterable<? extends CharSequence> lines, Charset charset, OpenOption... options) Writes lines to file 写入行到文件static voidwriteLines(Path path, Iterable<? extends CharSequence> lines, OpenOption... options) Writes lines to file (UTF-8) 写入行到文件(UTF-8)static voidwriteString(Path path, CharSequence content, Charset charset, OpenOption... options) Writes string to file 写入字符串到文件static voidwriteString(Path path, CharSequence content, OpenOption... options) Writes string to file (UTF-8) 写入字符串到文件(UTF-8)
-
Method Details
-
readBytes
Reads file as byte array 读取文件为字节数组- Parameters:
path- the file path | 文件路径- Returns:
- byte array | 字节数组
-
readString
-
readString
-
readLines
-
readLines
-
lines
-
lines
-
readFirstLine
-
writeBytes
Writes byte array to file 写入字节数组到文件- Parameters:
path- the file path | 文件路径bytes- the byte array | 字节数组options- the open options | 打开选项
-
writeString
public static void writeString(Path path, CharSequence content, Charset charset, OpenOption... options) Writes string to file 写入字符串到文件- Parameters:
path- the file path | 文件路径content- the content | 内容charset- the charset | 字符集options- the open options | 打开选项
-
writeString
Writes string to file (UTF-8) 写入字符串到文件(UTF-8)- Parameters:
path- the file path | 文件路径content- the content | 内容options- the open options | 打开选项
-
writeLines
public static void writeLines(Path path, Iterable<? extends CharSequence> lines, Charset charset, OpenOption... options) Writes lines to file 写入行到文件- Parameters:
path- the file path | 文件路径lines- the lines | 行charset- the charset | 字符集options- the open options | 打开选项
-
writeLines
public static void writeLines(Path path, Iterable<? extends CharSequence> lines, OpenOption... options) Writes lines to file (UTF-8) 写入行到文件(UTF-8)- Parameters:
path- the file path | 文件路径lines- the lines | 行options- the open options | 打开选项
-
append
Appends content to file 追加内容到文件- Parameters:
path- the file path | 文件路径content- the content | 内容
-
appendLines
Appends lines to file 追加行到文件- Parameters:
path- the file path | 文件路径lines- the lines | 行
-
exists
Checks if path exists 检查路径是否存在- Parameters:
path- the path | 路径options- the link options | 链接选项- Returns:
- true if exists | 如果存在返回true
-
isFile
Checks if path is a file 检查路径是否为文件- Parameters:
path- the path | 路径- Returns:
- true if file | 如果是文件返回true
-
isDirectory
Checks if path is a directory 检查路径是否为目录- Parameters:
path- the path | 路径- Returns:
- true if directory | 如果是目录返回true
-
isEmptyDirectory
Checks if directory is empty 检查目录是否为空- Parameters:
path- the directory path | 目录路径- Returns:
- true if empty | 如果为空返回true
-
isSymbolicLink
Checks if path is a symbolic link 检查路径是否为符号链接- Parameters:
path- the path | 路径- Returns:
- true if symbolic link | 如果是符号链接返回true
-
isHidden
Checks if path is hidden 检查路径是否隐藏- Parameters:
path- the path | 路径- Returns:
- true if hidden | 如果隐藏返回true
-
isReadable
Checks if path is readable 检查路径是否可读- Parameters:
path- the path | 路径- Returns:
- true if readable | 如果可读返回true
-
isWritable
Checks if path is writable 检查路径是否可写- Parameters:
path- the path | 路径- Returns:
- true if writable | 如果可写返回true
-
isSameFile
-
createFile
-
createDirectory
-
createDirectories
-
delete
Deletes a file or empty directory 删除文件或空目录- Parameters:
path- the path | 路径
-
deleteIfExists
Deletes file if exists 删除文件(如果存在)- Parameters:
path- the path | 路径- Returns:
- true if deleted | 如果删除成功返回true
-
deleteRecursively
Deletes directory recursively 递归删除目录- Parameters:
path- the directory path | 目录路径
-
copy
Copies a file 复制文件- Parameters:
source- the source path | 源路径target- the target path | 目标路径options- the copy options | 复制选项- Returns:
- target path | 目标路径
-
copyRecursively
Copies directory recursively 递归复制目录- Parameters:
source- the source directory | 源目录target- the target directory | 目标目录options- the copy options | 复制选项
-
move
Moves a file 移动文件- Parameters:
source- the source path | 源路径target- the target path | 目标路径options- the copy options | 复制选项- Returns:
- target path | 目标路径
-
size
Gets file size 获取文件大小- Parameters:
path- the file path | 文件路径- Returns:
- size in bytes | 字节大小
-
directorySize
Gets directory size recursively 递归获取目录大小- Parameters:
path- the directory path | 目录路径- Returns:
- total size in bytes | 总字节大小
-
getLastModifiedTime
-
setLastModifiedTime
-
list
-
walk
-
walk
-
glob
-
find
-
find
public static Stream<Path> find(Path start, int maxDepth, BiPredicate<Path, BasicFileAttributes> matcher) Finds files with custom matcher 使用自定义匹配器查找文件- Parameters:
start- the start directory | 起始目录maxDepth- the max depth | 最大深度matcher- the matcher | 匹配器- Returns:
- stream of matching paths | 匹配的路径流
-