• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Problem deleting files

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I want to delete a folder and its 5 files, I've been doing this...



But the launch.bat file doesn't get deleted (causing the last line to not delete the folder naturally as well)

However, if I print out the result of .delete(), as shown below, then all files including the folder delete.


So seems to be an issue with the threading perhaps, I don't want to rely on using System.out.println(...) to fix it,
as its a bit flaky, and doesn't address the issue, can anyone help?

Thanks
 
author & internet detective
Posts: 39343
755
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colin,
Are you allowed to use an open source library? Apache Commons has FilesUtils.deleteDirectory which takes care of all of this for you.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

Yes, thats fine, I can't seem to find a jar download for it.
 
Jeanne Boyarsky
author & internet detective
Posts: 39343
755
Eclipse IDE VI Editor Java
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I found it, and added to libraries, thanks
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually your link was more upto date, with 2.4, instead of 2.0, so I'll use that.

Cheers
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm,

Its still not working when I use FileUtils, it says..

java.io.IOException: Unable to delete file: C:\Users\colin\IconoSphere\i1\launch.bat

Which was the file it wasn't deleting before, this is annoying.
 
Jeanne Boyarsky
author & internet detective
Posts: 39343
755
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible that file is in use? It sounds like the operating system is having trouble deleting it.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so..

The java creates the bat file, just like a normal text file, and names it bat.
I'm not executing it at all, so I don't see the problem.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did something a bit mad...



And this seems to work, even if I've launched the bats before deleting them
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, the renaming doesn't always work, so can't use that.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I think I've fixed it...

I was writing the bat file using PrintWriter, and calling close on it later.
However, when trying to delete these bats manually, it was saying it was in use by java.

So I swapped back over to BufferedWriter...



And I'm able to delete the bats manually and by java, so I think its resolved now, fingers crossed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!