Skip to main content

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.
CreateNestedFolders.java
import java.io.*;

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())){
if(file.mkdirs())
System.out.println("Success ! Folders created.");
else
System.out.println("Failure ! Folders not created.");
}
}else{
System.out.println("PERMISSION DENIED");
}
}

}
Success ! Folders created.

Comments