提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在两个位置之间创建路径
import java.nio.file.Path; import java.nio.file.Paths; public class Main {//from 时 代 Java 公 众 号 - nowjava.com public static void main(String[] args) { Path firstPath; Path secondPath; firstPath = Paths.get("music/Future Setting A.mp3"); secondPath = Paths.get("docs"); System.out.println("From firstPath to secondPath: " + firstPath.relativize(secondPath)); System.out.println("From secondPath to firstPath: " + secondPath.relativize(firstPath)); System.out.println(); firstPath = Paths.get("music/Future Setting A.mp3"); secondPath = Paths.get("music"); System.out.println("From firstPath to secondPath: " + firstPath.relativize(secondPath)); System.out.println("From secondPath to firstPath: " + secondPath.relativize(firstPath)); System.out.println();//NowJava.com 提供 firstPath = Paths.get("music/Future Setting A.mp3"); secondPath = Paths.get("docs/users.txt"); System.out.println(