permaculture playing cards*
The moose likes Beginning Java and the fly likes File Deletion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "File Deletion " Watch "File Deletion " New topic
Author

File Deletion

kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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!


[Jess in Action][AskingGoodQuestions]
kayanaat sidiqui
Ranch Hand

Joined: Sep 04, 2008
Posts: 122
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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Deletion
 
Similar Threads
How Do I completely Empty My DIrectory?
About for loop of jdk1.5
KeyTool -genkeypair gives exception - "Keystore file exists, but is empty"
File sepearator for unix/windows problem
How to get all files in a subdirectory of a webpage