เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

จัดระเบียบโน้ตลงในโฟลเดอร์

เพื่อให้แอปเป็นระเบียบ ควรบันทึกโน้ตไว้ในโฟลเดอร์เฉพาะ ในแบบฝึกหัดนี้ จะสร้างไดเรกทอรีชื่อ "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());
        }
    }
}
แก้ไขและรันโค้ด