Class OpenPath

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

public final class OpenPath extends Object
Path Utility Class 路径工具类

Utility class for path operations including extension handling, normalization, and path manipulation.

用于路径操作的工具类,包括扩展名处理、规范化和路径操作。

Features | 主要功能:

  • Extension handling - 扩展名处理
  • Path normalization - 路径规范化
  • Path manipulation - 路径操作
  • Unique file generation - 唯一文件生成

Usage Examples | 使用示例:

// Get extension
String ext = OpenPath.getExtension(path); // "txt"

// Get name without extension
String name = OpenPath.getNameWithoutExtension(path); // "file"

// Change extension
Path newPath = OpenPath.changeExtension(path, "md");

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-io V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • getExtension

      public static String getExtension(Path path)
      Gets the file extension 获取文件扩展名
      Parameters:
      path - the file path | 文件路径
      Returns:
      extension without dot, empty string if none | 不含点的扩展名,无扩展名返回空字符串
    • getExtension

      public static String getExtension(String filename)
      Gets the file extension from filename 从文件名获取扩展名
      Parameters:
      filename - the filename | 文件名
      Returns:
      extension without dot | 不含点的扩展名
    • getNameWithoutExtension

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

      public static String getNameWithoutExtension(String filename)
      Gets filename without extension from filename string 从文件名字符串获取不含扩展名的文件名
      Parameters:
      filename - the filename | 文件名
      Returns:
      name without extension | 不含扩展名的文件名
    • changeExtension

      public static Path changeExtension(Path path, String newExtension)
      Changes file extension 更改文件扩展名
      Parameters:
      path - the original path | 原路径
      newExtension - the new extension (without dot) | 新扩展名(不含点)
      Returns:
      new path | 新路径
    • normalize

      public static Path normalize(Path path)
      Normalizes path 规范化路径
      Parameters:
      path - the path | 路径
      Returns:
      normalized path | 规范化后的路径
    • relativize

      public static Path relativize(Path base, Path target)
      Gets relative path 获取相对路径
      Parameters:
      base - the base path | 基准路径
      target - the target path | 目标路径
      Returns:
      relative path | 相对路径
    • resolve

      public static Path resolve(Path base, String other)
      Resolves path 解析路径
      Parameters:
      base - the base path | 基准路径
      other - the other path | 其他路径
      Returns:
      resolved path | 解析后的路径
    • getParent

      public static Path getParent(Path path)
      Gets parent directory 获取父目录
      Parameters:
      path - the path | 路径
      Returns:
      parent path | 父路径
    • getRoot

      public static Path getRoot(Path path)
      Gets root path 获取根路径
      Parameters:
      path - the path | 路径
      Returns:
      root path | 根路径
    • isAbsolute

      public static boolean isAbsolute(Path path)
      Checks if path is absolute 检查是否为绝对路径
      Parameters:
      path - the path | 路径
      Returns:
      true if absolute | 如果是绝对路径返回true
    • toAbsolute

      public static Path toAbsolute(Path path)
      Converts to absolute path 转换为绝对路径
      Parameters:
      path - the path | 路径
      Returns:
      absolute path | 绝对路径
    • toRealPath

      public static Path toRealPath(Path path)
      Gets real path (resolves symbolic links) 获取真实路径(解析符号链接)
      Parameters:
      path - the path | 路径
      Returns:
      real path | 真实路径
    • isSubPath

      public static boolean isSubPath(Path parent, Path child)
      Checks if child is a sub path of parent 检查子路径是否为父路径的子路径
      Parameters:
      parent - the parent path | 父路径
      child - the child path | 子路径
      Returns:
      true if sub path | 如果是子路径返回true
    • uniqueFile

      public static Path uniqueFile(Path dir, String baseName, String extension)
      Generates a unique filename in directory 在目录中生成唯一文件名
      Parameters:
      dir - the directory | 目录
      baseName - the base name | 基础名
      extension - the extension | 扩展名
      Returns:
      unique file path | 唯一文件路径
    • getFileName

      public static String getFileName(Path path)
      Gets file name from path 从路径获取文件名
      Parameters:
      path - the path | 路径
      Returns:
      file name | 文件名
    • 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
    • getNameCount

      public static int getNameCount(Path path)
      Gets path name count 获取路径名称数量
      Parameters:
      path - the path | 路径
      Returns:
      name count | 名称数量
    • join

      public static Path join(String first, String... more)
      Joins multiple path segments 连接多个路径段
      Parameters:
      first - the first path | 第一个路径
      more - the more paths | 更多路径
      Returns:
      joined path | 连接后的路径
    • join

      public static Path join(Path base, String... more)
      Joins path with more segments 连接路径与更多段
      Parameters:
      base - the base path | 基准路径
      more - the more paths | 更多路径
      Returns:
      joined path | 连接后的路径