MulaiMulai sekarang secara gratis

Verifying note locations

Before accessing a saved note, your app should confirm it's where it should be. In this final exercise, you'll check whether specific files and folders exist and print relevant messages, just like verifying shelves before filing paper notes.

Latihan ini adalah bagian dari kursus

Input/Output and Streams in Java

Lihat Kursus

Petunjuk latihan

  • Create a File object notesDir that points to notes.
  • Create a new directory named notes.
  • List all contents of the directory named notes.
  • Retrieve the absolute path of the file named "note.txt".

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

class DirectoryManager {

    public static void main(String[] args) {
        try {
            // Create a directory
            File notesDir = ____ ____("notes");
            if (____.____()) {
                System.out.println("Directory 'notes' created successfully");
            }

            File noteFile = new File("notes/note.txt");
            if (noteFile.createNewFile()) {
                System.out.println("File 'note.txt' created successfully");
            }

            // List contents of the directory
            File[] files = ____.____();
            if (files != null) {
                for (File f : files) {
                    System.out.println("File: " + f.getName());
                }
            }
            
			// Retrieve and print the absolute path of the file
            System.out.println("Absolute Path: " + ____.____());

        } catch (Exception e) {
            System.out.println("An error occurred: " + e.getMessage());
        }
    }
}
Edit dan Jalankan Kode