Verificar la ubicación de las notas
Antes de acceder a una nota guardada, tu app debería comprobar que está donde debe. En este ejercicio final, vas a verificar si existen archivos y carpetas concretos y a imprimir mensajes relevantes, igual que comprobarías las estanterías antes de archivar notas en papel.
Este ejercicio forma parte del curso
Entrada/Salida y Streams en Java
Instrucciones del ejercicio
- Crea un objeto
FilellamadonotesDirque apunte anotes. - Crea un directorio nuevo llamado
notes. - Lista todo el contenido del directorio llamado
notes. - Obtén la ruta absoluta del archivo llamado
"note.txt".
Ejercicio interactivo práctico
Prueba este ejercicio y completa el código de muestra.
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());
}
}
}