ComenzarEmpieza gratis

Almacenar notas recientes en caché

Para acelerar el acceso a las notas recientes, tu app las guardará en un archivo de caché ligero. En esta tarea, crearás un archivo nuevo llamado "cache.txt" y comprobarás que se ha creado correctamente. Esto sienta las bases para guardar datos de uso frecuente.

Todos los paquetes necesarios de java.io ya se han importado por ti.

Este ejercicio forma parte del curso

Entrada/Salida y Streams en Java

Ver curso

Instrucciones del ejercicio

  • Crea un objeto File para "cache.txt".
  • Comprueba si el archivo ya existe.
  • Si el archivo existe, elimínalo.
  • Crea un archivo nuevo llamado "cache.txt".

Ejercicio interactivo práctico

Prueba este ejercicio y completa el código de muestra.

class CacheDataManager {  

    public static void main(String[] args) {  
        try {  
			// Create a File object
            File cacheFile = ____ ____("cache.txt");  

            // Check if the file exists
            if (____.____()) {  
            	// Attempt to delete the file
                if (____.____()) {  
                    System.out.println("Old cache file deleted successfully.");  
                } else {  
                    System.out.println("Failed to delete the old cache file.");  
                }  
            }  

            // Create the file for cache.txt
            if (____.____()) {  
                System.out.println("New cache file created successfully.");  
            } else {  
                System.out.println("Failed to create the new cache file.");  
            }  

        } catch (IOException e) {  
            System.out.println("An error occurred: " + e.getMessage());  
        }  
    }  
}
Editar y ejecutar código