LoslegenKostenlos loslegen

Verifying note locations

Bevor deine App auf eine gespeicherte Notiz zugreift, sollte sie bestätigen, dass sie am richtigen Ort liegt. In dieser letzten Übung prüfst du, ob bestimmte Dateien und Ordner existieren, und gibst passende Meldungen aus — so wie man Regale überprüft, bevor man Papiernotizen ablegt.

Diese Übung ist Teil des Kurses

Ein-/Ausgabe und Streams in Java

Kurs anzeigen

Anleitung zur Übung

  • Erstelle ein File-Objekt notesDir, das auf notes zeigt.
  • Lege ein neues Verzeichnis namens notes an.
  • Liste alle Inhalte des Verzeichnisses notes auf.
  • Rufe den absoluten Pfad der Datei "note.txt" ab.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

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());
        }
    }
}
Code bearbeiten und ausführen