简体   繁体   中英

Directory isn't being deleted

What I want to happen is that when my project is completed the file that it created is deleted (so that all the files in it are deleted). What I do is

public void delete() {
    File f = new File (JavCapture.tmpLocation + "/tmp");
    if (f.isDirectory()) {
        f.delete();
        System.out.println("Deleted tmp folder!");
    }
}

Not only does this not delete the folder but it also doesn't do the print statement which means (well what I assume it means) is that the directory doesn't exist but it does.

Also I make the location using this.

new File(tmpLocation + "/tmp").mkdir();

You could try something like this:

    public static boolean delete(final File directory){
    assert directory != null && directory.exists();
    if(!directory.isDirectory())
        return directory.delete();
    for(final File f : directory.listFiles())
        delete(f);
    return directory.delete();
}

Only empty folder can be deleted with this method. Write a recursive method to remove recursively all files first and then the parent folder.

 void delete(File file) {
   if (file.isFile) 
      file.delete();
   else {
      for (File sub: file.listFiles()) 
        delete(sub);
      file.delete();
   }
 }

Most of the time, the semantics for deleting a directory are different than those for deleting a file. Usually, you need to recurse into the directory and delete everything in the directory before you can delete the directory.

As @Audrius said, you must first delete all files in a directory, and all directories in a directory, before you can delete the directory.

The print statement is not executing because the program is erroring out. If the file's 'delete' implementation is sane, you should be seeing an error message; have you suppressed errors in some way?

the problem is that you can't remove a directory if contains something. So you must delete recursively the content of directory!

In java 7:

public class DeleteDirVisitor extends SimpleFileVisitor<Path> {

private Path baseDir;

public DeleteDirVisitor() {
}

/**
 * If baseDir is specified all the subdirectory will be deleted but not basedir
 * 
 * @param baseDir
 */
public DeleteDirVisitor(Path baseDir) {
    this.baseDir = baseDir;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
    if ((baseDir == null) || !dir.equals(baseDir)) {
        Files.delete(dir);
    }
    return FileVisitResult.CONTINUE;
}

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

}

And use:

Files.walkFileTree(Paths.get(new File("Your Path").getAbsolutePath()), new DeleteDirVisitor());

您可以在Apache Commons IO中使用FileUtils.deleteDirectory()方法

In Java 8 I use this code to delete a folder and everything underneath it:

    Path path = Paths.get("tmp");
    if (Files.exists(path)) {
        List<Path> allFiles = new ArrayList<Path>();
        Files.walk(path).forEach(pa -> allFiles.add(pa));
        Collections.reverse(allFiles);
        allFiles.forEach(pa -> {try {System.out.println("deleting " + pa);Files.delete(pa);} catch(IOException e){}});
    }

Remove the System.out in the last line for silent deletion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM