Create directory along with required nonexistent parent directories


/*
   Create directory along with required nonexistent parent directories
   This Java example shows how to create a directory along with the required parent directories
   in the filesystem using mkdirs method of Java File class.
*/

import java.io.*;

public class CreateDirParentDir {

 
public static void main(String[] args) {
   
   
//create File object
   
File dir = new File("C://FileIO//Parent1//Parent2//DemoDir");
   
   
/*
     * To create directory along with the required nonexistent parent directories use,
     * boolean mkdirs() method of Java File class.
     *
     * This method returns true if the directory was created successfully along with
     * all necessary nonexistent parent directories, false otherwise.
     *
     * It may be possible that, some of the parent directories may have been created
     * eventhough the operation fails.
     * 
     */
    
    
boolean isDirCreated = dir.mkdirs();
    
    
if(isDirCreated)
      
System.out.println("Directory created along with required nonexistent parent directories");
    
else
      
System.out.println("Failed to create directory");
 
}
}

/*
Output would be
Directory created along with required nonexistent parent directories
*/