Java delete directory

Hi everyone!! Welcome to flower brackets blog. In this post you are going to learn java delete directory.

java delete directory

In java, if we want to delete a directory, compiler checks delete() method of File class to check if directory is empty or not.

Also read – create directory java program

If it is empty, directory gets deleted, else delete method will return false. So we have to recursively delete all the files. Now let’s see java program on how to delete a folder with files using java,

import java.io.*;

public class DeleteDirectoryDemo
{
   private static final String SRC_FOLDER = "D:\\project-demo";

   public static void main(String[] args)
   {
      File directory = new File(SRC_FOLDER);

      // checking directory
      if(!directory.exists())
      {
         System.out.println("Directory doesn't exist!!!");
         System.exit(0);
      }
      else
      {
         try                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   {        {  
            deleteDirectory(directory);
         }
         catch(IOException e)
         {
            e.printStackTrace();
            System.exit(0);
         }
      }
      System.out.println("deleted");
   }

   public static void deleteDirectory(File fl) throws IOException
   {
      if(fl.isDirectory())
      {
         // here directory is empty
         if(fl.list().length == 0)
         { 
            // deleted
            fl.delete();
            System.out.println("Directory is deleted : " + fl.getAbsolutePath());
         }
         else
         { 
            // listing directory contents
            String files[] = fl.list();

            for(String temp : files) 
            {
               // file structure constructor
               File fileDelete = new File(fl, temp);
               // deleting recursively
               deleteDirectory(fileDelete);
            }

            // checking the directory
            if(fl.list().length == 0)
            {
               // deleting
               fl.delete();
               System.out.println("Directory deleted : " + fl.getAbsolutePath());
            }
         }
      }
      else
      {
         // deleting file
         fl.delete();
         System.out.println("File deleted : " + fl.getAbsolutePath());
      }
   }
}


Related Posts