Java 學習記錄95 — Separators Temp Files and File Stores

張小雄
3 min readDec 15, 2021

--

String separator = File.separator;
System.out.println(separator);
separator = FileSystems.getDefault().getSeparator();
System.out.println(separator);

輸出結果:

\

\

獲取跟輸出分隔符的方法

在 unix 系統上運行會輸出 /

跟 windows 剛好相反(這邊的輸出結果就是 windows,我現在用 win10)

我一開始以為是 unix 在搞特別

但我又看了一下網際網路的網址

結果跟 unix 系統的一樣

原來是 windows 在搞特別

當初不知道為啥沒統一

搞到我們這些後代要用的時候還得注意不同系統的分隔符不同

還有系統結構也是如此

只有 windows 有 c、d、e… 那些槽

unix-like 的系統只有根目錄 /

我小時候剛開始碰電腦就是 windows

原本都覺得這樣很正常

學了編程以後接觸到了 Linux

才逐漸感覺到 unix-like 的系統設計好像比較直覺

這點在安裝檔案的時候特別有感覺

windows 要先選好某槽再選安裝地資料夾

unix-like 的系統只須直接選安裝地資料夾

try {
Path tempFile = Files.createTempFile("myapp", "appext");
System.out.println("Temporary file path = " + tempFile.toAbsolutePath());
} catch (IOException e) {
System.out.println(e.getMessage());
}

輸出結果:

Temporary file path = C:\Users\angel\AppData\Local\Temp\myapp6301496190125308909appext

創造一個暫時檔案

我去路徑上看了

真的有多了這個新檔案

Iterable<FileStore> stores = FileSystems.getDefault().getFileStores();
for (FileStore store : stores) {
System.out.println("Volume name / Drive letter = " + store);
System.out.println("file store = " + store.name());
}

輸出結果:

Volume name / Drive letter = (C:)

file store =

我不曉得為啥第二個啥都沒輸出

Tim 老師是用 Mac,第二個輸出滿多資料夾的

Iterable<Path> rootPaths = FileSystems.getDefault().getRootDirectories();
for(Path path:rootPaths){
System.out.println(path);
}

輸出結果:

C:\

E:\

輸出磁碟槽

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

--

--

張小雄
張小雄

Written by 張小雄

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

No responses yet