Create Directories in Java

Exercise:

Write a Java Program to create directories.

1. Create using the mkdir() method

Click Here to View the Solution!
import java.io.File;
 public class Main {
   public static void main(String[] args) {
  
     // creates a file object according to given path
     File file = new File("Java Example\\directory");
  
     // create a new directory
     boolean value = file.mkdir();
     if(value) {
       System.out.println("The new directory is created.");
     }
     else {
       System.out.println("The directory already exists.");
     }
   }
} 
Click Here to View the Output!
The new directory is created.
Click Here to View the Explanation!
  • This program deals with the creation of directories/ folders by employing mkdir(), a method of File class.  
  • The return value of this method is true indicating that a directory has been created.
  • Else, false is returned, in case if directory exists already.
  • In the first statement of main method a new object of File class is instantiated named file, passing (Java Example\\directory) as an argument specifying a path where the directory is supposed to be created.
  • In the next statement, mkdir() method is called upon file object; and its return value is assigned to a boolean variable value.
  • The ‘if’ condition checks if ‘value’ is true, ‘The new directory is created’. Message is displayed.
  • Otherwise, ‘The directory already exists.’ is printed.
  • One thing to be noticed here is that in case if there is no parent directory i.e. ‘Java Example’ as of now, then mkdir() will not be able to create a child directory at all.

2.Create using the mkdirs() method

Click Here to View the Solution!
 import java.io.File;
 public class Main {
   public static void main(String[] args) {
  
     // creates a file object in the current path
     File file = new File("Java Tutorial\\abc");
  
     // tries to create a new directory
     boolean value = file.mkdirs();
     if(value) {
       System.out.println("The new directory is created.");
     }
     else {
       System.out.println("The directory already exists.");
     }
   }
 } 
Click Here to View the Output!
The new directory is created.
Click Here to View the Explanation!
  • This program employs the method mkdirs() for the creation of directories/ sub-folders unlike in the previous example.
  • The execution steps of this program involve the creation of a File object  file specifying the path for the directory abc in the parent folder ‘Java Tutorial’.
  • The next step is to call mkdirs() method on file instance.
  • Then the return value is assigned to value, that helps if condition to check whether there exists a directory by the name specified in the parameter or not.
  • If a directory does not tend to exist, then ‘The new directory is created.’ Message is displayed.
  • In other case, ‘The directory already exists.’ is dispalyed.
  • The difference between mkdir and mkdirs is that, mkdir creates only the child directory whereas mkdirs creates parent directory also.
  • Moreover, the primary back-slash in path specification argument is dealt as an escape-character.