Home »
Java Programs »
Java Files & Directories Programs
Create directory along with required nonexistent parent directories in Java
Example of mkdirs() method: Here, we are going to learn how to create directory along with required nonexistent parent directories in Java?
Submitted by IncludeHelp, on July 19, 2019
Problem statement
The task is to create directory along with required nonexistent directories.
Creating directory along with required nonexistent parent directories
To create directory along with required nonexistent directory, we use mkdirs() method which is a predefined method of "File" class, it is called with the file object which is being initialized with the directories (along with the path) and returns a Boolean value – "true" if directories created successfully, otherwise it returns "false".
Package to use: import java.io.*;
Syntax
// create File object with directories
File dirs = new File("C://course//semester1//programming//java");
//method call to create directories
dirs.mkdirs();
Output:
true
Java program to create directory along with required nonexistent parent directories
// Java code to create directory along with required
// nonexistent parent directories
import java.io.*;
public class CreateDIRwithParentDIRs {
public static void main(String[] args) {
// create File object with directories
File dirs = new File("C://course//semester1//programming//java");
// variable to store result
// and method call to create directories
boolean result = dirs.mkdirs();
if (result)
System.out.println("Directories created sucessfully...");
else
System.out.println("Error occured...");
}
}
Output
Directories created sucessfully...
Java Files & Directories Programs »