提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
合并两个路径可以使您定义固定的根路径,并在其中附加部分路径。
import java.nio.file.Path; import java.nio.file.Paths; /* N o w J a v a . c o m 提 供 */ public class Main { public static void main(String[] args) { //define the fix path Path base_1 = Paths.get("C:/folder1/folder2/folder4"); Path base_2 = Paths.get("C:/folder1/folder2/folder3/test.txt"); //resolve test.txt file Path path_1 = base_1.resolve("test.txt"); System.out.println(path_1.toString()); /**来 自 时代Java公众号**/ //resolve test2.txt file Path path_2 = base_1.resolve("test2.txt"); System.out.println(path_2.toString());