This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes File does not delete fast enough Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "File does not delete fast enough" Watch "File does not delete fast enough" New topic
Author

File does not delete fast enough

Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
I have an app that listens for a certain file. The file is a JPEG that is about 669 KB in size. Once it finds it, it loads it. The app also deletes the file then listens for a new file with the same name to appear. The problem is that I cannot get the file deleted fast enough before the app wants to load it again. I have tried to put the deletion in a loop but it gets stuck there. Example:
while(!someFile.delete())
{
try
{
Thread.sleep(100);
}
catch(IOException ioe)
{
}
}
Any help will be greatly appreciated.
Warren Bell
Netricks
warren@netricks.net
Van Glass
Ranch Hand

Joined: Nov 18, 2000
Posts: 110
Sounds like the checking of the file and displaying of the file should be in two separate threads. Use of wait/notify in these threads would allow them to communicate with each other.
For example:
while(!somefile.delete()) {
wait();
}
Your other thread would fire a notify() when it is done displaying the image.
Hope this helps.
Lu Battist
Ranch Hand

Joined: Feb 17, 2003
Posts: 104
Double check your code to make sure that the file you are trying to delete was closed. If it is still open, the delete will not occur.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I like to use a method like this when I delete files - if there's a problem, the IOException makes it obvious, and may succeed in identifying the cause:

However the most common problem appears as "reason unknown" using this method. It's the one Lu suggests - some other stream, channel, or RandomAccessFile is still open for this file, and needs to be closed. In many cases garbage collection will take care of this for you, but often it's not as quick as you might like. So be usre to close streams, channels, and RandomAccessFiles as soon as you're done with them. Don't wait for GC.


"I'm not back." - Bill Harding, Twister
Warren Bell
Ranch Hand

Joined: Dec 20, 2000
Posts: 56
Thanks for all the suggestions, they really have helped.
Warren Bell
Netricks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File does not delete fast enough