How to delete non empty directory in java

Table of Contents

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

Was this post helpful?

Related Posts

  • Convert OutputStream to byte array in java
    16 February

    Convert Outputstream to Byte Array in Java

    Table of ContentsConvert OutputStream to Byte array in JavaConvert OutputStream to ByteBuffer in JavaWas this post helpful? In this post, we will see how to convert OutputStream to Byte array in Java. Convert OutputStream to Byte array in Java Here are steps to convert OutputStream to Byte array in java. Create instance of ByteArrayOutputStream baos […]

  • 27 December

    How to get current working directory in java

    Learn about how to get current working directory in java using different ways

  • 08 August

    Difference between Scanner and BufferReader in java

    Table of ContentsIntroductionScannerBufferedReaderDifference between Scanner and BufferedReaderWas this post helpful? In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major […]

  • 29 March

    Read UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferedReader()Using BufferedReaderUsing DataInputStream’s readUTF() method Was this post helpful? In this post, we will see how to read UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. There are multiple ways to […]

  • 29 March

    Write UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferWriter()Using BufferedWriterUsing DataOutputStream’s writeUTF() method Was this post helpful? In this post, we will see how to write UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. We will use Hindi language […]

  • 15 March

    Java read file line by line

    Table of ContentsJava 8 StreamsUsing BufferReaderUsing ScannerUsing FilesUsing RandomAccessFileUsing Apache CommonsWas this post helpful? In this post, we will see different ways to read file line by line in java. Sometimes, we need to read file line by line to a String, and process it. Here are different ways to read file line by line […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.