user2612619
user2612619

Reputation: 1129

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();

Upvotes: 0

Views: 171

Answers (6)

Clemens Fehr
Clemens Fehr

Reputation: 146

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.

Upvotes: 0

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21728

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();
   }
 }

Upvotes: 0

Simone Rondelli
Simone Rondelli

Reputation: 408

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());

Upvotes: 0

achyut
achyut

Reputation: 148

You can use the FileUtils.deleteDirectory() method in Apache Commons IO

Upvotes: 0

root
root

Reputation: 491

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?

Upvotes: 0

Josh M
Josh M

Reputation: 11939

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();
}

Upvotes: 1

Related Questions