Java教程

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

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

在本例中,我们将学习使用 String 方法、URI 类和 java.nio.file 包从 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
在上面的例子中,我们有两个名为 <var>absolutePath1</var> 和 <var>absolutePath2</var> 的绝对路径。我们已经使用 URI 类将绝对路径转换为相对路径。
toURI()-将 <code>File</code> 对象转换为 Uri relativize()-通过将两个绝对路径相互比较来提取相对路径 getPath()-将 Uri 转换为字符串
推荐阅读:
Java 文件 Java 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())
这里,<code>substring()</code> 方法返回 <var>absolutePath1</var> 从 index 开始的部分,等于 <var>absolutePath2。即,<var>absolutePath2</var> 表示的字符串从 <var>absolutePath1</var> 中移除。
要详细了解子字符串的工作原理,请访问 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
在上面的例子中,我们使用了<code>Path</code>接口的<code>relativize()</code>方法从两个绝对路径中获取了一个相对路径。
推荐阅读:
Java 路径类 Java 路径接口
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4