I have written method that recursively deletes directory with its contents. Code is executed on Windows - Eclipse - JVM7
Here's the code:
/**
* Empty and delete a folder with recursion.
*
* @param folder
* folder to empty
*/
public static boolean rmdir(final String folderPath, boolean deleteParent) {
File folder = new File(folderPath);
// check if folder file is a real folder
if (folder.isDirectory()) {
File[] list = folder.listFiles();
if (list != null) {
for (int i = 0; i < list.length; i++) {
File tmpF = list[i];
if (tmpF.isDirectory())
rmdir(tmpF.toString(), true);
tmpF.delete();
}
}
if (deleteParent && !folder.delete()) {
return false;
} else
return true;
}
return false;
}
When the code is executed no error is thrown, saying that directory has been deleted successfully. When I open drive to confirm deletion, I can still see the folder which is now 0 bytes in size with "Access denied" error when trying to open it.
EDIT:
I am administrator, have all permissions and can R/W to this folder from Windows explorer (before Java breaks it). My question is has anyone seen this before and what could be the cause of this??
One more thing: It seems that the directory gets set to "DeleteOnExit" instead of "Delete" right away. Because when program ends in Lotus Notes or Eclipse directory disappears.
Ok I got this resolved...
I was not Java issue, it was Lotus Notes/Domino issue.
Here comes the long sentence:
It seems when Lotus Notes/Domino thread creates folder "A" and some files in it using LotusScript mkdir
, rmdir
, and other IO methods and then executes Java VM which tries to delete folder "A" with all files in it, parent thread (Lotus Notes/Domino) gets corrupted and loses directory structure handle which results in error above.
Solution to the problem is simple Dir$()
statement after Java finishes folder deletion.
When Dir$()
is executed, phantom folder disappears.
Probably because the Dir$()
call refreshes and releases whatever should be refreshed and released internally.