• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Deletion

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am using following to delete all files that exist in "abc" directory.

void delete() {

final File logDir = new File("abc");
final File[] listOfFiles = logDir.listFiles();
for(File file : listOfFiles) {
if(file.isFile()) {
file.delete();
}
}
}

But nothing is get deleted.
Can you tell me where is the problem?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code looks good; you need to trace it and see what it's doing, mostly to find out if it's actually finding the files you expect it to find. You could run it under a debugger, or just add a print statement:



and then see what the output is!
 
kayanaat sidiqui
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Ernest Friedman-Hill ,
Tahnk you for quick reply.

Well i tried the same and getting this-

Tried to delete abc\file1 result = false
Tried to delete abc\file2 result = false

Now what?
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The "getAbsolutePath()" should have printed the entire path from the root. Are those exactly the files you expected it to find?

If so, then the "false" means that the JVM tried and failed to deleted the files. It could be a permissions issue, or you could be on Windows, where deleting an open file will fail. Since you said this is a log file directory, maybe you're trying to delete log files that are currently in use? You can't do that on Windows; you have to make sure the files are closed, first.
 
reply
    Bookmark Topic Watch Topic
  • New Topic