How to Delete a Directory/Folder in Java using Recursion With Examples

Earlier we learned how to create a file in java and how to delete a file in java. Here we will learn how to delete a non-empty directory/folder in java recursively.

Java Delete Directory

Java File delete() method deletes a file or an empty directory. However, if directory is not empty, it doesn’t delete it and returns false.

We will use delete() function recursively to delete a directory/folder in java program.

Here we are using delete() to delete the folder contents and if it’s directory, we are recursively calling recursiveDelete method.

Here is the output of the above program which deletes the directory.

You can use this utility method to delete a non-empty directory.

By admin

Leave a Reply

%d bloggers like this: