English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this example, we will learn how to delete an empty directory, a non-empty directory, and a directory with non-empty subdirectories in Java.
import java.io.File; 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("The directory has been deleted"); } else { System.out.println("Directory not found"); } } catch (Exception e) { e.getStackTrace(); } } }
In the above example, we used the delete() method of the File class to delete the directory namedDirectorydirectory.
Here, if the directory exists, the message will be displayedThe directory has been deleted. Otherwise, it will display " Directory not found.
For more information about files, please visitJava File.
In Java, to delete a non-empty directory, we must first delete all files existing in the directory. Then, we can delete the directory.
import java.io.File; class Main { public static void main(String[] args) { try { //Create a new file object File directory = new File("Directory"); //List all files in the 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("The directory has been deleted"); } else { System.out.println("Directory not Found"); } } catch (Exception e) { e.getStackTrace(); } } }
In the above example, we have used for-Iterate over the directory to delete all existing files. Once all files are deleted, use the delete() method to delete the directory.
import java.io.File; class Main { public static void deleteDirectory(File directory) { //Whether the file is a directory if(directory.isDirectory()) { File[] files = directory.listFiles(); //If the directory contains any files if(files != null) { for(File file : files) { //If the subdirectory is not empty, make a recursive call deleteDirectory(file); } } } if(directory.delete()) { System.out.println(directory + " has been deleted"); } else { System.out.println("The directory has not been deleted"); } } public static void main(String[] args) { try { //Create a new file object File directory = new File("Directory"); Main.deleteDirectory(directory); } catch (Exception e) { e.getStackTrace(); } } }
Here, assume we have a non-empty directory named Directory. The Directory contains2files named file1.txt and file2.txt and a non-empty subdirectory named Subdirectory. Similarly, Subdirectory contains a file named file11.txt.
Now, when we run the program, we will get the following output.
Directory\file1.txt has been deleted Directory\file2.txt has been deleted Directory\Subdirectory\file11.txt has been deleted Directory\Subdirectory has been deleted The directory has been deleted
Here, first delete2Delete the files in Subdirectory recursively. Once Subdirectory is empty, delete it. Finally, delete Directory.