I am trying to delete a file. it says that it exists, but it won't delete it. here is my code:
here is my output:Code:public static void editTask(String task, int taskNumber) throws IOException {
File inFile = new File("tasks.txt");
File tempFile = new File(inFile.getAbsolutePath() + ".txt");
BufferedReader br = new BufferedReader(new FileReader(inFile));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
while ((line = br.readLine()) != null) {
if (!line.trim().equals("Task3Ødo this!Ø1§")) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
// CHECK IF FILE EXISTS FIRST!!!! - PASSED
boolean success = inFile.exists();
if (success) {
System.out.println("file exists");
}
// TRY DELETING FILE - FAILED!
if (!inFile.delete()) {
System.out.println("Could not delete file");
}
}
Code:file exists
Could not delete file

