5

I am trying to walk the file tree and delet all files/directories. The code is below:

        Files.walkFileTree(metricPath, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file,
                                             BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir,
                                                      IOException exc) throws IOException {
                if (exc == null) {
                    Files.delete(dir);
                    return FileVisitResult.CONTINUE;
                } else {
                    throw exc;
                }
            }
        });
    }

This code is run in between unit tests, each of which is generating a separate file, in the form folder1/folder2/file. When I try to walk that tree, The DirectoryNotEmptyException is thrown when folder1 attempts to be deleted, although it is clearly empty...

Roman C
  • 49,761
  • 33
  • 66
  • 176
Bober02
  • 15,034
  • 31
  • 92
  • 178
  • possible duplicate of [Delete files recursively in Java](http://stackoverflow.com/questions/779519/delete-files-recursively-in-java) – jlordo Jan 11 '13 at 09:18
  • Actually, that other question was for when you have a File. In this case the code is doing it for Path, and it isn't necessarily possible to convert a Path into a File, so Commons IO's FileUtils for instance won't work. – Hakanai Apr 15 '13 at 02:05

4 Answers4

0

Have you checked that dir for hidden files? On Windows it could be that some process have opened this directory and opened file HANDLE still exists in system HANDLE table. In that case directory is locked and java could throw that exception.

Archer
  • 5,073
  • 8
  • 50
  • 96
  • Basically, the postVisit for folder2 still exists, when postVisit for folder1 is called. Looks like Java bug... – Bober02 Jan 10 '13 at 19:19
  • I doubt it's jave bug long living in JRE sources. I had to be solved long ago. Could you explain once again what you just said now `postVisit for folder1 still exists bla bla bla`. What is postVisit? – Archer Jan 10 '13 at 19:22
  • the postVisitDirectory method for folder1 fails, because there is folder2 within it... I do not know why the postVisit method is called for folder1 prior to calling that for folder1... – Bober02 Jan 10 '13 at 19:29
  • According to [this][1] javadoc postVisitDirectory is called only AFTER all entries inside been visited. However: ` This method is also invoked when iteration of the directory completes prematurely (by a visitFile method returning SKIP_SIBLINGS, or an I/O error when iterating over the directory).` [1]: http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileVisitor.html – Archer Jan 10 '13 at 19:35
0

As I can see it on your code, there should not be a problem, not unless one file/folder is in read only mode. you may want to explore changing the file permission first before deleting.

can you also try to put Files.delete() method on the following override

public FileVisitResult visitFileFailed(Path file, IOException exc)

Reference: JAVA NIO Directory Delete

0

Use Apache Commons FileUtils.deleteDirectory() or FileUtils.deleteQuietly()

Sourabh Bhavsar
  • 301
  • 3
  • 3
-2

Alternatively, you can import Apache Commons IO and use its FileUtils.deleteDirectory(File directory) method. Just one line is enough as it deletes all files and subdirectories recursively:

FileUtils.deleteDirectory(dirToBeDeleted);
matsev
  • 32,104
  • 16
  • 121
  • 156