Class MoreFiles
java.lang.Object
cloud.opencode.base.io.file.MoreFiles
More File Utilities - Advanced file operations
更多文件工具 - 高级文件操作
Provides advanced file operations including atomic writes, enhanced file tree traversal, and secure file operations.
提供高级文件操作,包括原子写入、增强的文件树遍历和安全文件操作。
Features | 主要功能:
- Atomic write operations - 原子写入操作
- Enhanced file tree traversal - 增强的文件树遍历
- Secure delete - 安全删除
- Touch operation - 创建或更新时间戳
- File equality checking - 文件相等性检查
Usage Examples | 使用示例:
// Atomic write (safe for concurrent access)
MoreFiles.writeAtomically(path, "content");
// Touch file (create if not exists, update timestamp if exists)
MoreFiles.touch(path);
// Delete recursively with filter
MoreFiles.deleteRecursively(dir, path -> path.toString().endsWith(".tmp"));
// File tree as stream with options
try (Stream<Path> files = MoreFiles.fileTree(dir)
.maxDepth(3)
.filter(Files::isRegularFile)
.stream()) {
files.forEach(System.out::println);
}
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Since:
- JDK 25, opencode-base-io V1.2.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classFile Tree Traversal Builder 文件树遍历构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancontentEquals(Path path1, Path path2) Checks if two files have equal content.static PathcreateParentDirectories(Path path) Creates parent directories if they don't exist.static booleandeleteDirectoryIfEmpty(Path path) Deletes a directory only if it's empty.static booleandeleteIfExists(Path path) Deletes if exists, returning whether deletion occurred.static voiddeleteRecursively(Path path) Deletes a file or directory recursively.static voiddeleteRecursively(Path path, Predicate<Path> filter) Deletes files recursively that match the filter.static MoreFiles.FileTreeTraversalCreates a file tree traversal builder.static StringgetFileExtension(Path path) Gets file extension (with dot).static StringgetNameWithoutExtension(Path path) Gets filename without extension.static booleanisEmptyDirectory(Path path) Checks if a directory is empty.Returns all directories under the path recursively.listFilesRecursively(Path path) Returns all files (not directories) under the path recursively.static voidCreates an empty file or updates its last modified time.static voidwalkFileTree(Path path, Consumer<Path> action) Walks the file tree and applies the action to each file.static voidwriteAtomically(Path path, byte[] data) Writes bytes atomically to a file.static voidwriteAtomically(Path path, InputStream input) Writes from an InputStream atomically to a file.static voidwriteAtomically(Path path, Iterable<String> lines) Writes lines atomically to a file (UTF-8).static voidwriteAtomically(Path path, Iterable<String> lines, Charset charset) Writes lines atomically to a file.static voidwriteAtomically(Path path, String content) Writes content atomically to a file (UTF-8).static voidwriteAtomically(Path path, String content, Charset charset) Writes content atomically to a file.
-
Method Details
-
writeAtomically
Writes content atomically to a file. 原子地将内容写入文件。This method writes to a temporary file first, then atomically moves it to the target location. This ensures that readers never see partially written content.
此方法先写入临时文件,然后原子地移动到目标位置。 这确保读者永远不会看到部分写入的内容。
- Parameters:
path- the target file path | 目标文件路径content- the content to write | 要写入的内容charset- the charset | 字符集
-
writeAtomically
-
writeAtomically
Writes bytes atomically to a file. 原子地将字节写入文件。- Parameters:
path- the target file path | 目标文件路径data- the bytes to write | 要写入的字节
-
writeAtomically
-
writeAtomically
-
writeAtomically
Writes from an InputStream atomically to a file. 原子地从 InputStream 写入文件。- Parameters:
path- the target file path | 目标文件路径input- the input stream | 输入流
-
touch
Creates an empty file or updates its last modified time. 创建空文件或更新其最后修改时间。Similar to Unix 'touch' command.
类似于 Unix 的 'touch' 命令。
- Parameters:
path- the file path | 文件路径
-
deleteRecursively
Deletes a file or directory recursively. 递归删除文件或目录。- Parameters:
path- the path to delete | 要删除的路径
-
deleteRecursively
-
deleteIfExists
Deletes if exists, returning whether deletion occurred. 如果存在则删除,返回是否发生删除。- Parameters:
path- the path to delete | 要删除的路径- Returns:
- true if the file was deleted | 如果文件被删除返回 true
-
deleteDirectoryIfEmpty
Deletes a directory only if it's empty. 仅当目录为空时删除。- Parameters:
path- the directory path | 目录路径- Returns:
- true if deleted | 如果已删除返回 true
-
fileTree
Creates a file tree traversal builder. 创建文件树遍历构建器。- Parameters:
root- the root directory | 根目录- Returns:
- a FileTreeTraversal builder | FileTreeTraversal 构建器
-
listFilesRecursively
-
listDirectoriesRecursively
-
walkFileTree
-
contentEquals
-
isEmptyDirectory
Checks if a directory is empty. 检查目录是否为空。- Parameters:
path- the directory path | 目录路径- Returns:
- true if empty | 如果为空返回 true
-
createParentDirectories
-
getFileExtension
-
getNameWithoutExtension
-