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

Rajendra Gutupalli Rajendra.Gutupalli at Sun.COM
Mon Aug 4 09:56:05 PDT 2008


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.

        at 
sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
        at 
sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
        at 
sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
        at sun.nio.fs.WindowsPath.delete(WindowsPath.java:771)
        at 
adhoc.TestWFT$CustomFileVisitor2.preVisitDirectory(TestWFT.java:44)
        at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:167)
        at java.nio.file.FileTreeWalker.walk(FileTreeWalker.java:58)
        at java.nio.file.Files.walkFileTree(Files.java:392)
        at java.nio.file.Files.walkFileTree(Files.java:416)
        at adhoc.TestWFT.test(TestWFT.java:27)
        at adhoc.TestWFT.main(TestWFT.java:19)
</Result>

<code>
import java.io.IOException;
import java.nio.file.*;
import static java.nio.file.FileVisitResult.*;
import java.nio.file.attribute.*;


public class TestWFT {

    public static void main(String... args) {
        TestWFT.test();

    }

    private static void test() {
        Path path = Paths.get("folder");
        try {
            path.createDirectory();
            Files.walkFileTree(path, new CustomFileVisitor2());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                path.delete(true);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    static class CustomFileVisitor2 implements FileVisitor {

        public FileVisitResult preVisitDirectory(FileRef dir, Path 
relPath) {
            try {
                dir.delete(true);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return CONTINUE;
        }

        public FileVisitResult visitFile(FileRef file, Path relPath, 
BasicFileAttributes attrs) {
            return CONTINUE;
        }

        public FileVisitResult postVisitDirectory(FileRef dir, Path 
relPath) {
            return CONTINUE;
        }

        public FileVisitResult visitFileFailed(FileRef file, Path 
relPath, IOException exc) {
            return CONTINUE;
        }

        public FileVisitResult visitDirectoryFailed(FileRef dir, Path 
relPath, IOException exc) {
            return CONTINUE;
        }
    }
}
</code>

Thanks
Rajendra..

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.openjdk.java.net/pipermail/nio-dev/attachments/20080804/fac89380/attachment.html 


More information about the nio-dev mailing list