-
Delete direcory
hi every body
i want to delete directory that may contain another directory or text file i use this recursive code when i send a directory that contain 2 text file method delete the second element in array and do not delete the first file and i do not know why?
Code:
public boolean delete(File f) {
if (f.exists()) {
File[] files = f.listFiles();
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
System.out.println(delete(files[i]));
} else {
System.out.println(files[i].delete());
}
}
}
return (f.delete());
}
-
You program logic is incorrect: if f exists it doesn't have to be a directory, it can be an ordinary file.
kind regards,
Jos
-
@Jos
That was my initial thought. Then I assumed that the initial call to the method is made with a known directory.
@OP
If you code is unable to delete a file then you may not have permissions to delete it or that some program (possibly the one you are currently running) has the file open. Make sure all files are closed before you try deleting them.
-
thanks
i have open files in my program so that method do not delete them
good luck