Java 程序:从两个绝对路径获取相对路径

示例 1:使用 URI 类从两个绝对路径获取相对路径

import java.io.File;
import java.net.URI;

class Main {
  public static void main(String[] args) {

    try {

      // Two absolute paths
      File absolutePath1 = new File("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
      System.out.println("Absolute Path1: " + absolutePath1);
      File absolutePath2 = new File("C:\\Users\\Desktop");
      System.out.println("Absolute Path2: " + absolutePath2);

      // convert the absolute path to URI
      URI path1 = absolutePath1.toURI();
      URI path2 = absolutePath2.toURI();

      // create a relative path from the two paths
      URI relativePath = path2.relativize(path1);

      // convert the URI to string
      String path = relativePath.getPath();

      System.out.println("Relative Path: " + path);


    } catch (Exception e) {
      e.getStackTrace();
    }
  }
}

输出

Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java
Absolute Path2: C:\Users\Desktop
Relative Path: Programiz/Java/Time.java

在上面的示例中,我们有两个名为 absolutePath1absolutePath2 的绝对路径。我们使用了 URI 类将绝对路径转换为相对路径。

  • toURI() - 将 File 对象转换为 Uri
  • relativize() - 通过比较两个绝对路径来提取相对路径
  • getPath() - 将 Uri 转换为 字符串

另请阅读:


示例 2:使用 String 方法从两个绝对路径获取相对路径

import java.io.File;

class Main {
  public static void main(String[] args) {

    // Create file objects
    File file1 = new File("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
    File file2 = new File("C:\\Users\\Desktop");

    // convert file objects to string
    String absolutePath1 = file1.toString();
    System.out.println("Absolute Path1: " + absolutePath1);
    String absolutePath2 = file2.toString();
    System.out.println("Absolute Path2: " + absolutePath2);

    // get the relative path
    String relativePath = absolutePath1.substring(absolutePath2.length());
    System.out.println("Absolute Path: " + relativePath);
  }
}

输出

Absolute Path1: C:\Users\Desktop\Programiz\Java\Time.java
Absolute Path2: C:\Users\Desktop
Absolute Path: \Programiz\Java\Time.java

在上面的示例中,我们已将文件路径转换为字符串。请注意此表达式:

absolutePath1.substring(absolutePath2.length())

在此,substring() 方法从等于 absolutePath2 长度的索引开始返回 absolutePath1 的一部分。也就是说,absolutePath2 所代表的字符串从 absolutePath1 中被移除。

要了解有关 substring 如何工作的更多信息,请访问 Java String substring()


示例 3:使用 java.nio.file 包从两个绝对路径获取相对路径

import java.nio.file.Path;
import java.nio.file.Paths;

class Main {
  public static void main(String[] args) {

    // Create file objects
    Path absolutePath1 = Paths.get("C:\\Users\\Desktop\\Programiz\\Java\\Time.java");
    Path absolutePath2 = Paths.get("C:\\Users\\Desktop");

    // convert the absolute path to relative path
    Path relativePath = absolutePath2.relativize(absolutePath1);
    System.out.println("Relative Path: " + relativePath);

  }
}

输出

Relative Path: Programiz\Java\Time.java

在上面的示例中,我们使用了 Path 接口的 relativize() 方法来从两个绝对路径获取相对路径。

另请阅读:

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战