Armazenando notas recentes em cache
Para acelerar o acesso às notas recentes, seu app vai armazená-las em um arquivo de cache leve. Nesta tarefa, você vai criar um novo arquivo chamado "cache.txt" e confirmar que ele foi criado com sucesso. Isso prepara o terreno para salvar dados usados com frequência.
Todos os pacotes necessários de java.io já foram importados para você.
Este exercicio faz parte do curso
Entrada/Saída e Streams em Java
Instruções do exercicio
- Crie um objeto
Filepara"cache.txt". - Verifique se o arquivo já existe.
- Se o arquivo existir, exclua-o.
- Crie um novo arquivo chamado
"cache.txt".
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
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());
}
}
}