จัดระเบียบโน้ตลงในโฟลเดอร์
เพื่อให้แอปเป็นระเบียบ ควรบันทึกโน้ตไว้ในโฟลเดอร์เฉพาะ ในแบบฝึกหัดนี้ จะสร้างไดเรกทอรีชื่อ "notes" และแสดงพาธของมัน เพื่อให้ไฟล์ที่เกี่ยวข้องอยู่รวมกันและค้นหาได้ง่าย
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Input/Output และ Streams ใน Java
คำแนะนำการฝึกหัด
- สร้างออบเจกต์
Fileชื่อnotesDirที่ชี้ไปยังnotes - สร้างไดเรกทอรีชื่อ
notesในระบบ - ดึง relative path ของไดเรกทอรีชื่อ
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());
}
}
}