Class OpenIO

java.lang.Object
cloud.opencode.base.io.OpenIO

public final class OpenIO extends Object
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 Details

    • readBytes

      public static byte[] readBytes(Path path)
      Reads file as byte array 读取文件为字节数组
      Parameters:
      path - the file path | 文件路径
      Returns:
      byte array | 字节数组
    • readString

      public static String readString(Path path, Charset charset)
      Reads file as string 读取文件为字符串
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      content string | 内容字符串
    • readString

      public static String readString(Path path)
      Reads file as UTF-8 string 读取文件为UTF-8字符串
      Parameters:
      path - the file path | 文件路径
      Returns:
      content string | 内容字符串
    • readLines

      public static List<String> readLines(Path path, Charset charset)
      Reads file as list of lines 读取文件为行列表
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      list of lines | 行列表
    • readLines

      public static List<String> readLines(Path path)
      Reads file as list of lines (UTF-8) 读取文件为行列表(UTF-8)
      Parameters:
      path - the file path | 文件路径
      Returns:
      list of lines | 行列表
    • lines

      public static Stream<String> lines(Path path, Charset charset)
      Returns a stream of lines from file 返回文件的行流
      Parameters:
      path - the file path | 文件路径
      charset - the charset | 字符集
      Returns:
      stream of lines | 行流
    • lines

      public static Stream<String> lines(Path path)
      Returns a stream of lines from file (UTF-8) 返回文件的行流(UTF-8)
      Parameters:
      path - the file path | 文件路径
      Returns:
      stream of lines | 行流
    • readFirstLine

      public static String readFirstLine(Path path)
      Reads the first line of file 读取文件首行
      Parameters:
      path - the file path | 文件路径
      Returns:
      first line or null | 首行或null
    • writeBytes

      public static void writeBytes(Path path, byte[] bytes, OpenOption... options)
      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

      public static void writeString(Path path, CharSequence content, OpenOption... options)
      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

      public static void append(Path path, CharSequence content)
      Appends content to file 追加内容到文件
      Parameters:
      path - the file path | 文件路径
      content - the content | 内容
    • appendLines

      public static void appendLines(Path path, Iterable<? extends CharSequence> lines)
      Appends lines to file 追加行到文件
      Parameters:
      path - the file path | 文件路径
      lines - the lines | 行
    • exists

      public static boolean exists(Path path, LinkOption... options)
      Checks if path exists 检查路径是否存在
      Parameters:
      path - the path | 路径
      options - the link options | 链接选项
      Returns:
      true if exists | 如果存在返回true
    • isFile

      public static boolean isFile(Path path)
      Checks if path is a file 检查路径是否为文件
      Parameters:
      path - the path | 路径
      Returns:
      true if file | 如果是文件返回true
    • isDirectory

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

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

      public static boolean isSymbolicLink(Path path)
      Checks if path is a symbolic link 检查路径是否为符号链接
      Parameters:
      path - the path | 路径
      Returns:
      true if symbolic link | 如果是符号链接返回true
    • isHidden

      public static boolean isHidden(Path path)
      Checks if path is hidden 检查路径是否隐藏
      Parameters:
      path - the path | 路径
      Returns:
      true if hidden | 如果隐藏返回true
    • isReadable

      public static boolean isReadable(Path path)
      Checks if path is readable 检查路径是否可读
      Parameters:
      path - the path | 路径
      Returns:
      true if readable | 如果可读返回true
    • isWritable

      public static boolean isWritable(Path path)
      Checks if path is writable 检查路径是否可写
      Parameters:
      path - the path | 路径
      Returns:
      true if writable | 如果可写返回true
    • isSameFile

      public static boolean isSameFile(Path path1, Path path2)
      Checks if two paths point to the same file 检查两个路径是否指向同一文件
      Parameters:
      path1 - the first path | 第一个路径
      path2 - the second path | 第二个路径
      Returns:
      true if same file | 如果是同一文件返回true
    • createFile

      public static Path createFile(Path path)
      Creates a file 创建文件
      Parameters:
      path - the file path | 文件路径
      Returns:
      created path | 创建的路径
    • createDirectory

      public static Path createDirectory(Path path)
      Creates a directory 创建目录
      Parameters:
      path - the directory path | 目录路径
      Returns:
      created path | 创建的路径
    • createDirectories

      public static Path createDirectories(Path path)
      Creates directories including parents 创建目录(包含父目录)
      Parameters:
      path - the directory path | 目录路径
      Returns:
      created path | 创建的路径
    • delete

      public static void delete(Path path)
      Deletes a file or empty directory 删除文件或空目录
      Parameters:
      path - the path | 路径
    • deleteIfExists

      public static boolean deleteIfExists(Path path)
      Deletes file if exists 删除文件(如果存在)
      Parameters:
      path - the path | 路径
      Returns:
      true if deleted | 如果删除成功返回true
    • deleteRecursively

      public static void deleteRecursively(Path path)
      Deletes directory recursively 递归删除目录
      Parameters:
      path - the directory path | 目录路径
    • copy

      public static Path copy(Path source, Path target, CopyOption... options)
      Copies a file 复制文件
      Parameters:
      source - the source path | 源路径
      target - the target path | 目标路径
      options - the copy options | 复制选项
      Returns:
      target path | 目标路径
    • copyRecursively

      public static void copyRecursively(Path source, Path target, CopyOption... options)
      Copies directory recursively 递归复制目录
      Parameters:
      source - the source directory | 源目录
      target - the target directory | 目标目录
      options - the copy options | 复制选项
    • move

      public static Path move(Path source, Path target, CopyOption... options)
      Moves a file 移动文件
      Parameters:
      source - the source path | 源路径
      target - the target path | 目标路径
      options - the copy options | 复制选项
      Returns:
      target path | 目标路径
    • size

      public static long size(Path path)
      Gets file size 获取文件大小
      Parameters:
      path - the file path | 文件路径
      Returns:
      size in bytes | 字节大小
    • directorySize

      public static long directorySize(Path path)
      Gets directory size recursively 递归获取目录大小
      Parameters:
      path - the directory path | 目录路径
      Returns:
      total size in bytes | 总字节大小
    • getLastModifiedTime

      public static Instant getLastModifiedTime(Path path)
      Gets last modified time 获取最后修改时间
      Parameters:
      path - the path | 路径
      Returns:
      last modified time | 最后修改时间
    • setLastModifiedTime

      public static void setLastModifiedTime(Path path, Instant time)
      Sets last modified time 设置最后修改时间
      Parameters:
      path - the path | 路径
      time - the time | 时间
    • list

      public static Stream<Path> list(Path dir)
      Lists directory contents 列出目录内容
      Parameters:
      dir - the directory path | 目录路径
      Returns:
      stream of paths | 路径流
    • walk

      public static Stream<Path> walk(Path start, int maxDepth)
      Walks directory tree 遍历目录树
      Parameters:
      start - the start directory | 起始目录
      maxDepth - the max depth | 最大深度
      Returns:
      stream of paths | 路径流
    • walk

      public static Stream<Path> walk(Path start)
      Walks directory tree with unlimited depth 遍历目录树(无深度限制)
      Parameters:
      start - the start directory | 起始目录
      Returns:
      stream of paths | 路径流
    • glob

      public static Stream<Path> glob(Path start, String pattern)
      Finds files matching glob pattern 查找匹配glob模式的文件
      Parameters:
      start - the start directory | 起始目录
      pattern - the glob pattern | glob模式
      Returns:
      stream of matching paths | 匹配的路径流
    • find

      public static Stream<Path> find(Path start, String regex)
      Finds files matching regex pattern 查找匹配正则表达式的文件
      Parameters:
      start - the start directory | 起始目录
      regex - the regex pattern | 正则表达式
      Returns:
      stream of matching paths | 匹配的路径流
    • 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 | 匹配的路径流