Delete a Directory in Java

Exercise:

Write a Java Program to delete empty and non-empty directory.

1.Delete an empty directory

Click Here to View the Solution!
import java.io.File;

public class Main {
    public static void main(String[] args) {

        try {
            // create a new file object
            File directory = new File("Directory");

            // delete the directory
            boolean result = directory.delete();

            if(result) {
                System.out.println("Directory Deleted");
            }
            else {
                System.out.println("Directory not Found");
            }

        } catch (Exception e) {
            e.getStackTrace();
        }
    }
}
Click Here to View the Output!
Here, if the directory is present, then the message Directory Deleted is shown. Otherwise, Directory not Found is shown.
Click Here to View the Explanation!
  • This program deals with the deletion process of an empty folder.
  • In the main method pass the name of the directory in the constructor of the instantiated object of File class.
  • The deletion process comprises of utilizing delete() method of File class.
  • One condition for using delete() method is that the directory should be empty before deletion.
  • As the focus of this program is to delete an empty directory, therefore delete() comes into play.
  • This method returns a boolean type value.
  •  If the intended directory exists in the location stored in the Path object, also it is empty then it is deleted.
  • In other case, a message is displayed indicating that the mentioned directory does not exist in the given path.
  • One thing to keep in mind that this method unlike in other languages, deletes files or directories permanently.

2.Delete a non- empty directory

Click Here to View the Solution!
public class Main {
   public static void main(String[] args) {
  
     try {
       // create a new file object
       File directory = new File("Directory");
  
       // list all the files in an array
       File[] files = directory.listFiles();
  
       // delete each file from the directory
       for(File file : files) {
         System.out.println(file + " deleted.");
         file.delete();
       }
  
       // delete the directory
       if(directory.delete()) {
         System.out.println("Directory Deleted");
       }
       else {
         System.out.println("Directory not Found");
       }
  
     } catch (Exception e) {
       e.getStackTrace();
     }
   } 
 }
Click Here to View the Output!
Here, if the directory is present, then the message Directory Deleted is shown. Otherwise, Directory not Found is shown.
Click Here to View the Explanation!
  • This program is a bit different from the previous one, in a manner that it deletes the files contained in the directory and then the directory itself.
  • For this purpose, the program constitutes of two portions.
  • First segment of the program deals with files deletion.
  • Pursuant to the previous example, first a File type object is instantiated.
  • After that all the files that exist in the directory are stored into a File type array.
  • For this purpose, listFiles() method of File class comes into play, which returns a list of all the files in a given directory.
  • The for-each loop iterates over every file in the array, one by one and deletes them by calling delete() method.
  • A message is also displayed, indicating the names of the files being deleted during each step.
  • After that delete() method is called again, upon the directory as it is empty now.
  • This time directory will be removed permanently.
%d bloggers like this: