2 Program to Delete a Directory - using File.delete and org.apache.commons.io.FileUtils's deleteDirectory(dir)- in java file IO


In this post we’ll write program to Delete a Directory - using File.delete and org.apache.commons.io.FileUtils's deleteDirectory(dir)- in java file IO >

  • Program 1 to Delete a Directory - org.apache.commons.io.FileUtils's deleteDirectory(dir) - in java file IO
  • Program 2 to Delete a Directory - using java.io.File.delete



Suppose you have to delete "c:/myDirectory" and your directory structure is like this


Program 1 to Delete a Directory - org.apache.commons.io.FileUtils's deleteDirectory(dir) - in java file IO >

org.apache.commons.io.FileUtils's deleteDirectory(deleteDirectory) method can also be used for deleting Directory.

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/** JavaMadeSoEasy.com */
public class DeleteDirectoryUsingCommonsJar {
  
   public static void main(String... args) {
          String deleteDirectoryName = "c:/myDirectory";
          File deleteDirectory = new File(deleteDirectoryName);
         
          if(deleteDirectory.isDirectory()){ //Ensure it is Directory                    
                 try {
                       FileUtils.deleteDirectory(deleteDirectory);
                       System.out.println(deleteDirectoryName +" deleted successfully");
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
          else{            
                 System.out.println(deleteDirectoryName +" isn't a Directory");
          }
         
   }
  
  
  
}
/* OUTPUT
c:/myDirectory deleted successfully
*/



Must read Directory operations - create, identify, copy, traverse.

Program 2 to Delete a Directory - using java.io.File.delete >

java.io.File's delete() method can be used for deleting Directory, but directory can only be deleted if its empty.
If it's not empty, then first we must delete all sub-directories and files present in that directory.

I have written method deleteSubDirAndFiles() method recursively deletes all sub-directories and files  present in that directory

import java.io.File;
import java.io.IOException;
/** JavaMadeSoEasy.com */
public class DeleteDirectory {
  
   public static void main(String... args) {
          String deleteDirectoryName = "c:/myDirectory";
          File deleteDirectory = new File(deleteDirectoryName);
         
          if(deleteDirectory.isDirectory()){ //Ensure it is Directory  
                
                 try {
                       deleteSubDirAndFiles(deleteDirectory);
                 } catch (IOException e) {
                       e.printStackTrace();
                 }
          }
          else{            
                 System.out.println(deleteDirectoryName +" isn't a Directory");
          }
         
   }
  
  
  
   /*
   *  deleteSubDirAndFiles() method recursively deletes all sub-directories and files
   *  present in that directory
   */
    public static void deleteSubDirAndFiles(File deleteDirectory) throws IOException {
          //if directory - enter if statement
          if (deleteDirectory.isDirectory()) {
                 //check whether Directory is empty
                 //If Directory is empty, delete it
                 if (deleteDirectory.list().length == 0) {
                       deleteDirectory.delete();
                       System.out.println("Directory is deleted : "
                                     + deleteDirectory.getAbsolutePath());
                 }
                 else { //else Directory isn't empty
                      
                       //obtain all sub-directories and files in allSubDirAndFiles[]
                       String allChild_SubDirAndFiles[] = deleteDirectory.list();
                       //iterate over allSubDirAndFiles[] i.e. iterate over
                       //sub-directories and files
                       for (String child_DirOrFileToBeDeleted : allChild_SubDirAndFiles) {
                              //recursively delete all sub-directories and files
                              deleteSubDirAndFiles(
                                       new File(deleteDirectory, child_DirOrFileToBeDeleted));
                       }
                       // Now, again check whether Directory is empty or not
                       //if Directory is empty then delete it
                       if(deleteDirectory.list().length == 0) {
                              deleteDirectory.delete();
                              System.out.println("Directory deleted > "
                                            + deleteDirectory.getAbsolutePath());
                       }
                 }
          }
          else {            //else it is file and delete that file
                 deleteDirectory.delete();
                 System.out.println("File deleted > " + deleteDirectory.getAbsolutePath());
          }
   }
}
/* OUTPUT
File deleted > c:\myDirectory\myFile1.txt
File deleted > c:\myDirectory\myFile2.txt
File deleted > c:\myDirectory\myFile3.txt
File deleted > c:\myDirectory\myFile4.txt
File deleted > c:\myDirectory\sub-directory\sub-directory myFile1.txt
File deleted > c:\myDirectory\sub-directory\sub-directory myFile2.txt
File deleted > c:\myDirectory\sub-directory\sub-directory myFile3.txt
File deleted > c:\myDirectory\sub-directory\sub-directory myFile4.txt
Directory deleted > c:\myDirectory\sub-directory
Directory deleted > c:\myDirectory
*/


RELATED LINKS>

Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO


Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file


Copy a Directory



Program to Write to file using FileWriter in java file IO


Must read for you :

No comments:

Post a Comment