views:

120

answers:

1

Hi,

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();
}
+3  A: 

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.

Carl Smotricz
I've added to my initial description the way the txt file is generated.I think the first time the file is really deleted, the problem is that inmediately I try to create it again with new information and I'm not closing it correctly.
dedalo
OK, I get it. Maybe you should be closing `pw` and not `f`. Or if you want to be very sure, both, in that order.
Carl Smotricz
I have reviewed every method used and I found I had not closed a FileReader. Now that it is fixed it works correctly. Thank you for your advice.
dedalo
Woo hoo, good work!
Carl Smotricz