0

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?

missingfaktor
  • 90,905
  • 62
  • 285
  • 365

1 Answers1

1

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130