missingfaktor
missingfaktor

Reputation: 92046

File#delete not deleting files

I need to delete a directory containing some files. I am using the following code:

public static void delete(File f) {
  if (f.isDirectory()) {
    for (File c : f.listFiles()) {
      delete(c);
    }
  }
  f.setWritable(true);
  f.delete();
}

For some reason, some files inside the directory, and hence the directory does not get deleted. What could be the possible reasons for this behavior, and how can I solve this problem?

Upvotes: 0

Views: 654

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533510

It could be that the file is open somewhere, assuming you have write permisions to the directory. Trying to delete a file which hasn't been properly closed is a common source of strange failures to delete. After the program exists you find that the file can be deleted.

Upvotes: 1

Related Questions