ADVERTISEMENT
ADVERTISEMENT

Create a directory in Java

Create directory in Java: Here, we are going to learn how to create a directory in java programming language?
Submitted by IncludeHelp, on July 18, 2019

The task is to create a directory in java.

Creating a directory

To create a directory, firstly we have to create a file object by passing the path of the directory where we have to create the directory and directory name.

The mkdir() method is used to create the directory, it is called with the file object that has the path where we have to create the directory and directory name, and returns a Boolean value (true – if directory created successfully, false – if the directory is not created).

Syntax:

    //file object creation by passing the path 
    File file = new File("d://course");

    //creating directory named "course" in "d://" drive
    file.mkdir();

    Output:
    true

Java code to create a directory

//Java code to create a directory  

import java.io.*;

public class Main {
  public static void main(String[] args) {
    //file object creation by passing the path 
    //where we have to create the directory
    File file = new File("d://course");

    //variable to store the result
    //assigning false as an initial value
    boolean result = false;

    //creating directory named "course" in "d://" drive
    result = file.mkdir();

    if (result == true) {
      System.out.println("Directory created successfully...");
    } else {
      System.out.println("Directory is not created...");
    }
  }
}

Output

Directory created successfully...

Java Files & Directories Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

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.