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 the life of me I don't see how/where it is answered!
So, on to my problem.
I am at work and am stuck using Java 6 SE and cannot upgrade to 7. I am writing a program that creates a file, writes to it, does some processing, and then needs to delete the file out of existence. I am having the exact same problem as the person who asked the question I reference above: Java will not delete the file and I cannot figure out why.
The code:
File f = null;
FileWriter fw = null;
try
{
f = new File("myFile.txt");
fw = new FileWriter(f);
fw.write("This is a sentence that should appear in the file.");
fw.flush();
if(f.delete())
System.out.println("File was successfully deleted.");
else
System.err.println("File was not deleted.");
}
catch(Exception exc)
{
System.err.println(exc.getMessage());
}
catch(Error er {
System.err.println(er.getMessage());
}
catch(Throwable t)
{
System.err.println(t.getMessage());
}
finally
{
fw.close();
}
It is not throwing any throwables, errors or exceptions (I included those to rule out any and all edge cases). The second print statement ("File was not deleted."
) is being printed to the console. I am running this on Windows 7 and am writing to a folder where I have full permissions (rwx).
The user asking the question I referenced answered his own question, but does so (in my humble opinion) in a not-so-straight-forward way. In any case, I am having trouble making sense of it. He/she seems to be alluding to the fact that using a BufferedReader
as opposed to a FileInputStream
made the difference for him/her, but I just don't see how that applies.
Java 7 seems to have fixed this issues with the introduction of a java.nio.file.Files
class, but again, I can't use Java 7 for reasons outside the scope of my control.
Other answerers to that referenced question allude that this is a "bug" in Java, and give all sorts of circumventions, such as explicitly calling System.gc()
, etc. I have tried all of these and they are not working.
Maybe someone can add a fresh perspective and jog some thinking for me.
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.");
You can only delete the file if there is no file handler left opened. Since you open the file hanlder using FileWriter
, you will need to close it before you can delete it. In other word, f.delete
must be executed after fw.close
Try the code below. I made the changes to prevent all possible bug you may found, e.g if fw is null.
File f = null;
FileWriter fw = null;
try {
f = new File("myFile.txt");
fw = new FileWriter(f);
fw.write("This is a sentence that should appear in the file.");
fw.flush(); // flush is not needed if this is all your code does. you data
// is automatically flushed when you close fw
} catch (Exception exc) {
System.err.println(exc.getMessage());
} finally {// finally block is always executed.
// fw may be null if an exception is raised in the construction
if (fw != null) {
fw.close();
}
// checking if f is null is unneccessary. it is never be null.
if (f.delete()) {
System.out.println("File was successfully deleted.");
} else {
System.err.println("File was not deleted.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With