How to delete a Folder in Java

Points To Remember

Before deleting the file you need to check if

  • you have permission to delete file using method canWrite()
  • file specified exists exists()

Deleting the file in Java

Test.java
import java.io.*;

class Test{

   public static void main(String args[]){

     File file = new File("/home/ekansh/test");
     if(file.isDirectory()){
        if(file.delete())
     System.out.println("Success ! Folder deleted.");
        else
     System.out.println("Failure ! Folder not deleted.");
     }else
 System.out.println("FOLDER DOES NOT EXIST");
      
   }

}
Success ! Folder deleted.

No comments :

Post a Comment