How to create Nested folders in Java

Points To Remember

You need to check the following things fro creating the nested folders

  • If it is a directory by method isDirectory()
  • If it exists by method exists()
  • You might need to check if you have write permissions to create folder by method canWrite()

How to create Nested Folders/Directories in Java

This is how yo can create the nested folders in java.

class CreateNestedFolders{

   public static void main(String args[]){

     File file = new File("/home/ekansh/test/1/2/3/abc");
     if(!file.canWrite()){ // check if user have write permissions
       if(!(file.exists() && file.isDirectory())){
          System.out.println("Success ! Folders created.");
          System.out.println("Failure ! Folders not created.");
 System.out.println("PERMISSION DENIED");

Success ! Folders created.

No comments :

Post a Comment