how to delete a directory in java

Solutions on MaxInterview for how to delete a directory in java by the best coders in the world

showing results for - "how to delete a directory in java"
Alexander
26 Feb 2016
1// delete a directory in java using recursion or recursively
2import java.io.File;
3public class DeleteDirectoryRecursively
4{
5   public static void main(String[] args)
6   {
7      String strFolder = "B:\New folder";
8      //delete folder recursively
9      deleteRecursive(new File(strFolder));
10   }
11   public static void deleteRecursive(File fl)
12   {
13      if(!fl.exists())
14         return;
15      // if directory call recursively
16      if(fl.isDirectory())
17      {
18         for(File file : fl.listFiles())
19         {
20            // recursive call
21            deleteRecursive(file);
22         }
23      }
24      // delete files and empty directory
25      fl.delete();
26      System.out.println("Deleted file/directory: " + fl.getAbsolutePath());
27   }
28}
Marie
11 Nov 2018
1boolean deleteDirectory(File directoryToBeDeleted) {
2    File[] allContents = directoryToBeDeleted.listFiles();
3    if (allContents != null) {
4        for (File file : allContents) {
5            deleteDirectory(file);
6        }
7    }
8    return directoryToBeDeleted.delete();
9}
queries leading to this page
delete foler javajava nio delete directoryjava 8 delete directoryjava delete a directory with fileshow to remove dir in java file methodjava delete directoryjava delete directory with filesdelete directories javahow to delete a folder in javajava how to remove directoryhow to delete files directory in javafiles delete directory javajava remove folderjava delete folder with filesdelete dir javajava delete file directoryhow to delete directory javadelete directory with files javajava files remove directoryhow to delete a directory with files in it in javaremove folder in javadelete a directory javadelete folder and content javajava delete current directoryhow to delete folder in javaforce delete directory in javajava remove directory and all fileshow to delete folders in javajava files delete directoryhow to delete a folder using javajava delete entire directoryjava delete all files from dirjava remove directoryhow to deleate folder i javahow to delete a directory in javadelete forder javadelete dir content in javajava delete folder and its contentsremove a directory in javahow to delete a folder with javajava force delete directoryremove directory javajava path delete directoryjava how to delete files in a directoryjava delete folderdelete directory in java 8how to get a directory path in javaremove folder javadelete folder contents javajava 8 delete directory and contentsdelete a directory in javapurgedirectory javajava 9 delete directory delete directory javajava bin folderdelete folders javadelete directory in javaif condition java delete a folder from directorydelete folder content javahow to force delete a folder in javajava delete folder recursivelyjava delete folder windowsjava delete windows folderhow to force delete a folder in using javahow to delete a directory recursively in java 8how to delete directory in java can you delete a directory with files in java 3ferase directory javadelete folder using javadelete folder in javajava delete directory filesjava delete folder and contentsjava how to recursively delete a directoryjava delete folder and contents windowsdelete entire directory javadelete dir in java how to delete a folder with files in it javajava delete folder in pathhow to delete directory and subdirectories in javao delete a directory in javadelele folder javahow to force delete directory in javadelete a folder in javadelete folder javajava remove directory and filesdelete dir as file javadelte directory in javahow to delete folder javajava how to delete file and directoryhow to delete a directory in java