Class MoreFiles

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

public final class MoreFiles extends Object
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:
  • Method Details

    • writeAtomically

      public static void writeAtomically(Path path, String content, Charset charset)
      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

      public static void writeAtomically(Path path, String content)
      Writes content atomically to a file (UTF-8). 原子地将内容写入文件(UTF-8)。
      Parameters:
      path - the target file path | 目标文件路径
      content - the content to write | 要写入的内容
    • writeAtomically

      public static void writeAtomically(Path path, byte[] data)
      Writes bytes atomically to a file. 原子地将字节写入文件。
      Parameters:
      path - the target file path | 目标文件路径
      data - the bytes to write | 要写入的字节
    • writeAtomically

      public static void writeAtomically(Path path, Iterable<String> lines, Charset charset)
      Writes lines atomically to a file. 原子地将行写入文件。
      Parameters:
      path - the target file path | 目标文件路径
      lines - the lines to write | 要写入的行
      charset - the charset | 字符集
    • writeAtomically

      public static void writeAtomically(Path path, Iterable<String> lines)
      Writes lines atomically to a file (UTF-8). 原子地将行写入文件(UTF-8)。
      Parameters:
      path - the target file path | 目标文件路径
      lines - the lines to write | 要写入的行
    • writeAtomically

      public static void writeAtomically(Path path, InputStream input)
      Writes from an InputStream atomically to a file. 原子地从 InputStream 写入文件。
      Parameters:
      path - the target file path | 目标文件路径
      input - the input stream | 输入流
    • touch

      public static void touch(Path path)
      Creates an empty file or updates its last modified time. 创建空文件或更新其最后修改时间。

      Similar to Unix 'touch' command.

      类似于 Unix 的 'touch' 命令。

      Parameters:
      path - the file path | 文件路径
    • deleteRecursively

      public static void deleteRecursively(Path path)
      Deletes a file or directory recursively. 递归删除文件或目录。
      Parameters:
      path - the path to delete | 要删除的路径
    • deleteRecursively

      public static void deleteRecursively(Path path, Predicate<Path> filter)
      Deletes files recursively that match the filter. 递归删除匹配过滤器的文件。
      Parameters:
      path - the path to delete from | 要删除的起始路径
      filter - the filter for files to delete | 要删除的文件过滤器
    • deleteIfExists

      public static boolean deleteIfExists(Path path)
      Deletes if exists, returning whether deletion occurred. 如果存在则删除,返回是否发生删除。
      Parameters:
      path - the path to delete | 要删除的路径
      Returns:
      true if the file was deleted | 如果文件被删除返回 true
    • deleteDirectoryIfEmpty

      public static boolean deleteDirectoryIfEmpty(Path path)
      Deletes a directory only if it's empty. 仅当目录为空时删除。
      Parameters:
      path - the directory path | 目录路径
      Returns:
      true if deleted | 如果已删除返回 true
    • fileTree

      public static MoreFiles.FileTreeTraversal fileTree(Path root)
      Creates a file tree traversal builder. 创建文件树遍历构建器。
      Parameters:
      root - the root directory | 根目录
      Returns:
      a FileTreeTraversal builder | FileTreeTraversal 构建器
    • listFilesRecursively

      public static Stream<Path> listFilesRecursively(Path path)
      Returns all files (not directories) under the path recursively. 递归返回路径下的所有文件(不包括目录)。
      Parameters:
      path - the root path | 根路径
      Returns:
      stream of files | 文件流
    • listDirectoriesRecursively

      public static Stream<Path> listDirectoriesRecursively(Path path)
      Returns all directories under the path recursively. 递归返回路径下的所有目录。
      Parameters:
      path - the root path | 根路径
      Returns:
      stream of directories | 目录流
    • walkFileTree

      public static void walkFileTree(Path path, Consumer<Path> action)
      Walks the file tree and applies the action to each file. 遍历文件树并对每个文件应用操作。
      Parameters:
      path - the root path | 根路径
      action - the action to apply | 要应用的操作
    • contentEquals

      public static boolean contentEquals(Path path1, Path path2)
      Checks if two files have equal content. 检查两个文件内容是否相等。
      Parameters:
      path1 - first file | 第一个文件
      path2 - second file | 第二个文件
      Returns:
      true if content is equal | 如果内容相等返回 true
    • isEmptyDirectory

      public static boolean isEmptyDirectory(Path path)
      Checks if a directory is empty. 检查目录是否为空。
      Parameters:
      path - the directory path | 目录路径
      Returns:
      true if empty | 如果为空返回 true
    • createParentDirectories

      public static Path createParentDirectories(Path path)
      Creates parent directories if they don't exist. 如果父目录不存在则创建。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the path | 路径
    • getFileExtension

      public static String getFileExtension(Path path)
      Gets file extension (with dot). 获取文件扩展名(带点)。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the extension or empty string | 扩展名或空字符串
    • getNameWithoutExtension

      public static String getNameWithoutExtension(Path path)
      Gets filename without extension. 获取不带扩展名的文件名。
      Parameters:
      path - the file path | 文件路径
      Returns:
      the filename without extension | 不带扩展名的文件名