How to delete non empty directory in java

Previous
Next
In this post, we will see how to delete┬áDirectory/Folder which is non empty. You can use java.io.File ‘s delete empy folder but you can not delete it if it is non empty.
There are multiple ways to do it.

  • Using java recursion
  • Using Apache common IO

Using java recursion:

It is very straight forward to do it. It will iterate through all files in the folder. If it is a file then we can directly delete it but if It finds folder inside it then we again call same method.

Using Apache common IO:

Delete non empty folder is very straight forward. You just need to call FileUtils.deleteDirectory() method.

Java Program:

When I ran above program, I got following output

Previous
Next

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.

Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog