• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File does not delete fast enough

 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Warren Bell
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the suggestions, they really have helped.
Warren Bell
Netricks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic