I’m working in an application using Tomcat and servlets.
There is a servlet (audioProcess) that receives the name of an mp3 file, that is played. This servlet also processes the mp3 file and stores the result in a txt file. The content of this txt will be replaced when the audioProcess servlet is invoked again (with a different mp3 file).
Everytime this servlet is invoked I delete the txt and I create a new one (containing information regarding the mp3 file). The name of the txt file is always the same.
My problem is that the txt file is never deleted. What I do to delete the file is:
File a = new File(path_clasificacion);
if(a.exists())
{
boolean erased = a.delete();
out.println("erased?" + erased);
}
The first time I invoke the servlet the txt file is erased. Only the first time. After deleting the txt file I try to write the new information in a txt file with the same name (stored in the same path). What happens is that the old file is not erased, the new information is added to the file.
Why can’t I delete the file?
Thanks
The txt file is generated using this code:
FileWriter f = null;
PrintWriter pw = null;
try
{
f = new FileWriter(path,true);
pw = new PrintWriter(f);
pw.println(info);
} catch (Exception e) {e.printStackTrace();
finally
{
f.close();
}
You can’t erase the file so long as you have an open file handle to it.
Look through your code and be sure to
close()any streams or readers. This obviously includes making sure any references to it from MP3 digesting library code are also closed.“What about the first time?” you will ask. Answer: You got a positive status from the
erase()call because the deletion request was successfully passed to the operating system. However, the OS is still twiddling its thumbs waiting for you to finally let go of the file. Thus, the file was not really (physically) deleted even the first time around.