Helping Information

Java Program to Create Directories

Java Program to Create Directories

Problem Definition: Java Program to Create Directories.

In a programming language java is use to produce a folders. It’s easier in java using mkdir() Method.

In this example, we will learn about a java program to create a folders. 

On the positive side, if you don’t have latest version of Java click here

The Java File class provides the “mkdir()” method to create a new directory. The method returns.
  • “true” if the new directory is create.
  • “false” if the directory already exists.
 

What is Directory?

A directory or file is used to store, classify and separate files and directories on a system. Let’s see, if you have a huge number of files having different types. You could have a directory to store them and likewise create a different directory for storing files. Usually, we are creating directories for a different type like music, video and documents. You could easily navigate through the files.

Java Program to Create Directories
Create a New Folder

1. Example: Java Program to Create Directories.

import java.io.File;

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

    // creates a file object with specified path
    File file = new File("Java Example\directory");

    // tries to create a new directory
    boolean value = file.mkdir();
    if(value) {
      System.out.println("The new directory is created.");
    }
    else {
      System.out.println("The directory already exists.");
    }
  }
}

As shown above example, we have create a file object name “file”. The object includes information about the specific directory path. 

File file = new File("Java Example\directory");

On this side, we have use the “mkdir()” method to create a new directory in the specific path.

Similarly, If the directory doesn’t exist in the specific location. The new directory is create and this message is shown.

The new directory is create.

However, if the directory already exists. we will see this message. 

The directory already exists. 

It is important to note that, the directory is create inside the Java Example parent directory.

In order to, if the Java Example parent directory doesn’t exist. Then the “mkdir()” method cannot create the directory. 

On this side, we can use the “mkdirs()” method of the Java “File” class. The method allows us to create the parent directory. As well as if it’s not already there.

2. Example: Java Program to Create Directories using mkdirs() Method.

import java.io.File;
class Main {
public static void main(String[] args) {
    // creates a file object in current path
  File file = new File("Java Tutorial\abc");
    // tries to create a new directory
    boolean value = file.mkdirs();
    if(value) {
      System.out.println("The new directory is created.");
    }
    else {
      System.out.println("The directory already exists.");
    }
  }
}

In the above example, we have create a file object name file. The object includes information about the directory path.

File file = new File("Tutorial\directory");

Additionally, we have use the “mkdirs()” method. On this side, we create a new directory with the specified path.

whereas, If the directory doesn’t exist in the current location. The new directory is create and this message is shown.

The new directory is create.

However, if the directory already exists. We will see this message.

The directory already exists.

Here, if the Tutorial directory doesn’t exist. Then the “mkdirs()” method creates the Tutorial directory as well along with the directory.

Note: We have use double-backslash while specifying the path. It is because the character is use as an escape character in Java. Henceforth, the first backslash is use as an escape character for the second one. 

Related Post:

  1. Java Program to Copy File
  2. Java Program to Rename File
  3. Java Program to List Files in a Directory
  4. Java Program to Find ASCII Value of a character.

Leave a Reply

Your email address will not be published. Required fields are marked *