Java 學習記錄90 — Filesystem

張小雄
7 min readDec 8, 2021

--

今天學的是 Filesystem

如何找到檔案和讀取

首先先創三個文件檔

內容隨便填寫做出區隔就行

1、目前專案新增 b 文檔

2、目前專案新增一個文件夾,裡面開一個 a 文檔

3、目前專案的上一層,新增 c 文檔

以我自己為例

a 文檔:files.txt

File within file.txt111111111111111111111

b 文檔:workingDirectory.txt

File within workingDirectory.txt22222222222222222222222222222222

c 文檔:outFileFromChapter14.txt

File within outFileFromChapter1433333333333333333333333333333333package nonBlockingIO.path;import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
Path path = FileSystems.getDefault().getPath("src/nonBlockingIO/path/workingDirectory.txt");
printFile(path);
// Path filePath = FileSystems.getDefault().getPath("src/nonBlockingIO/path/files","files.txt");
Path filePath = FileSystems.getDefault().getPath("src/nonBlockingIO/path/files/files.txt");
printFile(filePath);
filePath = Paths.get("C:\\software\\JetBrains\\IdeaProjects\\java-the-complete-java-developer-course\\outFileFromChapter14.txt");
printFile(filePath);
}
private static void printFile(Path path) {
try (BufferedReader fileReader = Files.newBufferedReader(path)) {
String line;
while ((line = fileReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

輸出結果:

File within workingDirectory.txt

22222222222222222222222222222222

File within file.txt

111111111111111111111

File within outFileFromChapter14

33333333333333333333333333333333

按理說 b 文檔 workingDirectory 這個文檔,用 getPath() 時,應該不用加前綴地址

因為前面 getDefault() 是當前預設位置,但不管我把檔案移到哪邊都無法達成

google 後找到方法了

// check working directory path 1
System.out.println("Working Directory = " + System.getProperty("user.dir"));
// check working directory path 2
Path path = Paths.get("");
System.out.println("Working Directory = " + path.toAbsolutePath());

輸出結果:

Working Directory = C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14

Working Directory = C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14

果然是檔案位置放錯了

直接用這兩個方法任選一個,去確認預設位置在哪再把檔案移過去就行

教學:How to get the current working directory in Java?

結果我看下一節影片,老師馬上就講了,如何確認當前工作路徑

老師是教方法二

Path path = FileSystems.getDefault().getPath("src/nonBlockingIO/path/files/files.txt");// different way
Path path = Paths.get("./src/nonBlockingIO/path","files", "files.txt");

又教了一個新方法,但我看起來沒有比較簡單

第一個參數是當前專案位置,第二個是檔案所在的資料夾,第三個是檔案

順序不能搞錯喔

. 代表當前資料夾

.. 代表上一層資料夾

Path path2 = FileSystems.getDefault().getPath(".", "src", "nonBlockingIO", "path", "files", "..", "files", "files.txt");
System.out.println(path2.normalize().toAbsolutePath());
printFile(path2);

輸出結果:

C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14\src\nonBlockingIO\path\files\files.txt

File within file.txt

111111111111111111111

用比較麻煩的方式來定位到檔案

確保自己學會了層級關係

System.out.println(path2.normalize().toAbsolutePath());
System.out.println(path2.toAbsolutePath());

輸出結果:

C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14\src\nonBlockingIO\path\files\files.txt

C:\software\JetBrains\IdeaProjects\java-the-complete-java-developer-course\chapter14\.\src\nonBlockingIO\path\files\..\files\files.txt

老師沒有講 normalize() 是幹麻的

自己測了一下,就是把 . 跟 .. 去掉而已,讓人比較容易看得懂

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

--

--

張小雄
張小雄

Written by 張小雄

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

No responses yet