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 »





Comments and Discussions!

Load comments ↻





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