ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » Java programs » Java file and directory 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

The task is to create directory along with required nonexistent 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 code 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...
ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.