Path path3 = FileSystems.getDefault().getPath("thisfiledoesntexist.txt");
System.out.println(path3.toAbsolutePath());
輸出結果:
C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14\thisfiledoesntexist.txt
這個路徑是不存在的,但還是可以打印出來
Path path4 = Paths.get("Z:\\", "abc", "don", "thisfiledoesntexist.txt");
System.out.println(path4.toAbsolutePath());
輸出結果:
Z:\abc\don\thisfiledoesntexist.txt
一樣是虛構的路徑
System.out.println("path3 exits = " + Files.exists(path3));
System.out.println("path4 exits = " + Files.exists(path4));
輸出結果:
path3 exits = false
path4 exits = false
用這方法確認路徑是否真的存在