將筆記整理到資料夾
為了保持整潔,你的應用程式應該把筆記存到專用的資料夾。這個練習中,你會建立一個名為 "notes" 的目錄,並印出它的路徑。這樣可以把相關檔案放在一起,方便尋找。
本練習屬於課程
Java 的輸入/輸出與串流
練習說明
- 建立一個指向
notes的File物件notesDir。 - 在系統中建立名為
notes的目錄。 - 取得名為
notes的目錄的相對路徑。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
class DirectoryManager {
public static void main(String[] args) {
try {
// Create a File object to represent the directory
File notesDir = ____ _____("notes");
// Create a directory in the system
if (____.____()) {
System.out.println("Directory 'notes' created successfully");
}
// Retrieve and print the relative path of the directory
System.out.println("Relative Path: " + ____.____());
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
}
}