i want delete a file in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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 Answers
+ 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