Domanda

I've got the following code to delete a folder and its contents but its only deleting the contents and not the folder. How can i change it to delete the folder as well?

private static void deleteCat(String catName) {
        File fileToDel = new File("Catalogues/" + catName);
        if (fileToDel.exists()) {
            if (fileToDel.isDirectory()) {
                if (fileToDel.list().length > 0) {
                    for (String s : fileToDel.list()) {
                        deleteCat(catName + "/" + s);
                    }
                }
            } else {
                if (fileToDel.delete()) {
                    System.out.println("File " + fileToDel + " deleted");
                } else {
                    System.out.println("Unable To Del " + fileToDel);
                }
            }

        }
    }
È stato utile?

Soluzione

remove the else of line 10. fileToDel.delete() must be executed anycase.

Altri suggerimenti

When you're trying to delete a directory, you've not included the code to delete the directory itself

private static void deleteCat(String catName) {
    File fileToDel = new File("Catalogues/" + catName);
    if (fileToDel.exists()) {
        if (fileToDel.isDirectory()) {
            if (fileToDel.list().length > 0) {
                for (String s : fileToDel.list()) {
                    deleteCat(catName + "/" + s);
                }
            }
            fileToDel.delete();
        }  
        //Always want to delete the given file, so remove the 'else'
        if (fileToDel.delete()) {
            System.out.println("File " + fileToDel + " deleted");
        } else {
             System.out.println("Unable To Del " + fileToDel);
        }
    }
}

It deletes only your content, because you don't delete the directories. Add at the end of your directoryroutine the command to delete your directory too.

if (fileToDel.isDirectory()) {
    if (fileToDel.list().length > 0) {
            for (String s : fileToDel.list()) {
                deleteCat(catName + "/" + s);
            }
    }
    fileToDel.delete(); //delete directory
} else ...

Also have a look at the apache commons-io lib (http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#deleteDirectory(java.io.File))

You can use the below code to delete a folder, all sub-folders, and all files inside that folder without using any 3rd party.

public static void deleteDirectoryUtil(Path folderPath) throws IOException {
    Files.walkFileTree(folderPath, 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 {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
    });
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top