failed to delete an empty folder while traversing Files.walkFileTree()(win)

Alan Bateman Alan.Bateman at Sun.COM
Mon Aug 4 10:38:07 PDT 2008


Rajendra Gutupalli wrote:
> Hi Alan,
>
> I don't know the exact behavior here but I am just wondering why the 
> exception is being thrown only in Windows.
> Here I am creating a folder and passed this empty folder to 
> Files.walkFileTree(). In preVisitDirectory() when I try to delete this 
> empty folder I got exception saying the file is being
> opened by another process. There is no other process opened this 
> folder. It is newly created empty folder. Is it okay to throw this 
> exception?
>
> <Result>
> java.nio.file.FileSystemException: folder: The process cannot access 
> the file because it is being used by another process.
Windows doesn't allow directories that have an open search handle to be 
deleted. I don't think we can do anything about it. In this case, a 
search handle is open until all the entries in the directory have been 
visited. Are you trying to do a recursive delete? If so, then the usual 
way to do this is to delete the directory after you have deleted all its 
entries - ie: move the the delete from the preVisitDirectory to the 
postVisitDirectory method and is should work as expected.

-Alan.




More information about the nio-dev mailing list