Arnold Cristobal
Arnold Cristobal

Reputation: 851

Java delete folder still exists

I have a block of code:

List<String> unusedFolders = new ArrayList<String>();

for (int i = 0; i < unusedFolders.size(); i++) {
    File dirDate = new File(unusedFolders.get(i));

    String[] files = dirDate.list();

    for(String f: files) {
        File file = new File(dirDate.getPath(),f);
        file.delete();
    }

    dirDate.delete();
    //dirDate.deleteOnExit();
    }

The list "unusedFolder" has values like:

C:\opt\transmission\tejas\transform\inv\20140927
C:\opt\transmission\tejas\transform\inv\20140928
C:\opt\transmission\tejas\transform\inv\20140929

It ran with no errors in eclipse but when I check in the windows explorer, the folders are still there and when I try to access it (click). It prompts:

C:\opt\transmission\tejas\transform\inv\20140927 is not accessible. Access is Denied.

Now, I can't even delete it manually. Anyone has an idea? thank you.

Upvotes: 0

Views: 426

Answers (2)

SamDJava
SamDJava

Reputation: 267

This most certainly the work of a WIndows environment. Some other process must be holding the lock to the folder or its contents. Once you have identified the process which is holding the lock and kill it, u will certainly be able to delete the folder.

If ur not sure of the process that is accesing the folder , u might consider restarting ur machine.

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70909

Typically when something like this happens, it happens on Windows.

That's because Windows can enforce a type of filesystem lock where the file won't disappear until it is no longer used. Check to see if you have an old copy of "something" running which might be keeping the file (in this case, the directory) open. If so, killing the program will typically allow the delete to complete.

That's assuming it is not something rather simple, like directory write permissions on the parent directory (which must be rewritten for a child directory to be removed).

Upvotes: 1

Related Questions