1

I try to delete a directory using java,here is my code

 public static void delDirectory(String path) throws IOException {
    Path p = Paths.get(path);
    delHelp(p);
}

private static void delHelp(Path p) throws IOException {
    if (!p.toFile().exists()) {
        return;
    } else if(p.toFile().isFile()){
        log.debug("delete file:" + p.toAbsolutePath().toString());
        Files.delete(p);
    }else if(p.toFile().isDirectory()){
        for(Path subPath:Files.newDirectoryStream(p)){
            delHelp(subPath);
        }
        log.debug("delete directory:"+p.toAbsolutePath().toString());
        Files.delete(p);
    }
}

On unix-like system, it works out. On windows, the code Files.delete(p) actually move the directory to the trash can, so when delete the parent directory the code will throw exception: Exception in thread "main" java.nio.file.DirectoryNotEmptyException

Any idea about this os-dependent behavior? How can I work around this?

Jade Tang
  • 321
  • 4
  • 23
  • It sounds so strange. As far as I know recycler bin works only when you use it with windows explorer. Which library are you using? – acostela Oct 08 '15 at 08:12
  • Are you sure that the issue is caused by the Trash Can? As far as I know (and reading at other posts) it could be possible just by deleting all subdirectories and files: http://stackoverflow.com/questions/29872608/how-to-bypass-java-nio-file-directorynotemptyexception – Andrea Oct 08 '15 at 08:12
  • Is it `java.nio.file.Files.delete()` and are you sure it moves the file to trash? On my system (Win7) it deletes it directly, and in general people seem to have the opposite problem regarding trash bin (i.e. making it work). – Cinnam Oct 08 '15 at 08:16
  • yes,I use import java.nio.file.Files to delete directory. And every thing works fine on my mac – Jade Tang Oct 08 '15 at 08:25

2 Answers2

2

The actual problem is that you are not closing the DirectoryStream, which is causing the DirectoryNotEmptyException when you try to delete the directory.

From the Javadoc:

When not using the try-with-resources construct, then directory stream's close method should be invoked after iteration is completed so as to free any resources held for the open directory.

So you can either call close() on it when you are done with it, or use it in try-with-resources:

private static void delHelp(Path p) throws IOException {
    if (!p.toFile().exists()) {
        return;
    } else if(p.toFile().isFile()){
        Files.delete(p);
    } else if(p.toFile().isDirectory()){

        try (DirectoryStream<Path> ds = Files.newDirectoryStream(p)) {
            for (Path subPath : ds){
                delHelp(subPath);
            }
        }

        Files.delete(p);
    }
}
Cinnam
  • 1,892
  • 1
  • 15
  • 23
0

please first of all add this Jar into your project first.

Find below code works perfectly as per your requirement too.

i.e. work on window machine and should be not goes to trash/recycle-bin

   public static void main(String[] args) {
            try {
                delDirectory("E:\\RecursiveDataContainDirectoryName");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        public static void delDirectory(String path) throws IOException {
                Path p = Paths.get(path);
                FileDeleteStrategy.FORCE.delete(p.toFile());
        }
Vishal Gajera
  • 4,137
  • 5
  • 28
  • 55