I am aware that this question is a raging duplicate of this question. However, I have now read that entire page twice, and some sections 3 times, and for th
You're trying to delete() a file which is still referenced by an active, open FileWriter.
Try this:
f = new File("myFile.txt");
fw = new FileWriter(f);
fw.write("This is a sentence that should appear in the file.");
fw.flush();
fw.close(); // actually free any underlying file handles.
if(f.delete())
System.out.println("File was successfully deleted.");
else
System.err.println("File was not deleted.");