Home » Java programming language

Java File Class boolean mkdirs() method with Example

Java File Class boolean mkdirs() method: Here, we are going to learn about the boolean mkdirs() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 16, 2019

File Class boolean mkdirs()

  • This method is available in package java.io.File.mkdirs().
  • This method is used to create a directory with all needed or necessary parent directories using mkdirs() method and this method is accessible with file object.
  • If the directory already exists so we are not allowed to create a directory of the same name.
  • The return type of this method is Boolean i.e. it returns true or false if return true means directory is successfully created with including all necessary parent directories. Else return false.

Syntax:

    boolean mkdirs(){
    }

Parameter(s):

We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is int, it returns true or false if true then the directory is successfully created with all parent directories else return false directory already exists or an exception occurs.

Java program to demonstrate example of mkdirs() method

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class CreateDirectoryUsingMkdirsMethod {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File dir1 = new File("E:\\Java");
            File dir2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");

            // By using mkdirs()create a new directory named 
            // C:\\Users\\Articles including all parent directories because 
            // directory is not exists(i.e it returns true) .
            if (dir1.mkdirs())
                System.out.println("Directory created Successfully " + dir1.getName());
            else
                System.out.println("Directory already exists " + dir1.getName());

            // By using mkdirs() is not create a new directory named 
            // " C:\\Users\\computer clinic\\OneDrive\\Articles " 
            // because the name of this directory is already exists 
            // (i.e. it returns false) so we will get a message 
            // Directory already exists .
            if (dir2.mkdirs())
                System.out.println("Directory created Successfully " + dir2.getName());
            else
                System.out.println("Directory already exists " + dir2.getName());

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

E:\Programs>javac CreateDirectoryUsingMkdirsMethod.java

E:\Programs>java CreateDirectoryUsingMkdirsMethod
Directory created Successfully Java
Directory already exists Articles


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.