Locaties van notities verifiëren
Voordat je een opgeslagen notitie opent, moet je app controleren of die staat waar hij hoort. In deze laatste oefening ga je na of bepaalde bestanden en mappen bestaan en print je relevante berichten, net zoals je planken controleert voordat je papieren notities opbergt.
Deze oefening maakt deel uit van de cursus
Input/Output en streams in Java
Oefeninstructies
- Maak een
File-objectnotesDirdat verwijst naarnotes. - Maak een nieuwe map met de naam
notes. - Geef alle inhoud weer van de map met de naam
notes. - Haal het absolute pad op van het bestand met de naam
"note.txt".
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
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());
}
}
}