i want delete a file in java | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

i want delete a file in java

i want to delete a file using the delete() method but it doesn't work. I create the file in another class. my code: username = scanner.nextLine(); File file = new File(username + ".txt"); if(file.exists()){ file.delete(); if (file.delete()){ System.out.println("your account deleted."); }else System.out.println("no"); }else System.out.println("username is wrong");

4th Jun 2023, 5:18 AM
3axpa
3axpa - avatar
2 Respostas
+ 9
Use this method Files.deleteIfExists(Paths.get(filepath));
4th Jun 2023, 5:34 AM
AĶ¢J
AĶ¢J - avatar
+ 4
@AĶ¢J Thank you!!!
4th Jun 2023, 7:39 AM
3axpa
3axpa - avatar