Java 學習記錄91 — exists

張小雄
Dec 9, 2021

--

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

用這方法確認路徑是否真的存在

上面代碼全都紀錄在我的 Github

--

--

張小雄
張小雄

Written by 張小雄

記錄成為軟體工程師的過程

No responses yet