Verificando locais das notas
Antes de acessar uma nota salva, seu app deve confirmar se ela está no lugar certo. Neste exercício final, você vai verificar se arquivos e pastas específicos existem e imprimir mensagens relevantes — como conferir as prateleiras antes de arquivar notas em papel.
Este exercicio faz parte do curso
Entrada/Saída e Streams em Java
Instruções do exercicio
- Crie um objeto
FilechamadonotesDirque aponte paranotes. - Crie um novo diretório chamado
notes. - Liste todo o conteúdo do diretório chamado
notes. - Obtenha o caminho absoluto do arquivo chamado
"note.txt".
exercicio interativo prático
Tente este exercicio completando este código de exemplo.
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());
}
}
}