File mkdirs() in Java

mkdirs(): This method is available in the java.io.File class of Java.

Syntax:

boolean java.io.File.mkdirs()

This method creates the directory named by this abstract pathname, including any necessary but non-existent parent directories.

Parameters: NA

Returns: true if and only if the directory was created, along with all necessary parent directories; false otherwise.

Throws:

SecurityException -  If a security manager exists and its java.lang.SecurityManager.checkRead(java.lang.String) method does not permit verification of the existence of the named directory and all necessary parent directories.

Approach 1: When the directory is created.

Java

import java.io.File;

public class Filemkdirs {
    public static void main(String[] args) {

        String pathname = "D:\\hello2.txt";
        File file = new File(pathname);

        System.out.println(file.mkdirs());
    }
}

Output:

true


Approach 2: When the directory is not created.

Java

import java.io.File;

public class Filemkdirs {
    public static void main(String[] args) {

        String pathname = "D:\\hello.txt";
        File file = new File(pathname);

        System.out.println(file.mkdirs());
    }
}

Output:

false


No comments:

Post a Comment