1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/* 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 */ |
Add Comment