how to get a directory path in java code example

Example: How to delete a directory in java

// delete a directory in java using recursion or recursively
import java.io.File;
public class DeleteDirectoryRecursively
{
   public static void main(String[] args)
   {
      String strFolder = "B:\New folder";
      //delete folder recursively
      deleteRecursive(new File(strFolder));
   }
   public static void deleteRecursive(File fl)
   {
      if(!fl.exists())
         return;
      // if directory call recursively
      if(fl.isDirectory())
      {
         for(File file : fl.listFiles())
         {
            // recursive call
            deleteRecursive(file);
         }
      }
      // delete files and empty directory
      fl.delete();
      System.out.println("Deleted file/directory: " + fl.getAbsolutePath());
   }
}

Tags:

Java Example