F
F
fandorin_official2019-11-12 10:00:50
Java
fandorin_official, 2019-11-12 10:00:50

How to delete all folders (with files laid out in them) and files from a folder?

The context is the following: there is one folder. It contains several files, as well as several folders in which, in turn, there are several files.
How can I completely empty this folder?
At the moment I tried to implement it through the following code:

recursiveDelete(new File("C:/ProgramData/Ex"));

    public static void recursiveDelete(File file) throws NullPointerException {
        if (!file.exists())
            return;
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                recursiveDelete(f);
            }
        }
        file.delete();
        System.out.println("Удаленный файл или папка: " + file.getAbsolutePath());
    }

But it only deletes some files, but does it not get about files in folders? How can this code be modified to clear the folder?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sergey, 2019-11-12
kuzmin @sergueik

fandorin_official is the job you want to do for you or do you need to delete the directory recursively?
Class org.apache.commons.io.FileUtils
https://commons.apache.org/proper/commons-io/javad...
static void deleteDirectory(File directory)
Deletes a directory recursively.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question