How to Create Directory in Java

Mohd Ebad Naqvi Feb 02, 2024
  1. Use the mkdir() Function to Create Directories in Java
  2. Use the mkdirs() Function to Create Directories in Java
  3. Use the createDirectory() Function to Create Directories in Java
  4. Use the createDirectories() Function to Create Directories in Java
  5. Use the forceMkdir() Function to Create Directories in Java
How to Create Directory in Java

A directory is a location or file system cataloging structure for storing files on your computer. We work with different file handling operations in Java. A file object is used to create a new folder or directory.

In this tutorial, we will create a directory in Java.

Use the mkdir() Function to Create Directories in Java

First, the instance of the file class is created. Then the parameter is passed to this instance, which is the directory path we want to make. Finally, the mkdir() method is invoked using the file object, creating the required directory.

For example,

import java.io.File;
import java.util.Scanner;
public class CreateDirectory {
  public static void main(String args[]) {
    System.out.println("Path of Directory? ");
    Scanner obj = new Scanner(System.in);
    String path = obj.next();
    System.out.println("Directory Name? ");
    path = path + obj.next();
    File D = new File(path);
    boolean D1 = D.mkdir();
    if (D1) {
      System.out.println("Directory is created successfully");
    } else {
      System.out.println("Error !");
    }
  }
}

Output:

Path of Directory?
C:/
Directory Name? 
TestDirectory
Directory is created successfully

Use the mkdirs() Function to Create Directories in Java

The drawback of the mkdir() method is that Java can only use it to create a single directory at a time. This drawback is resolved by using mkdirs() method as we can use it to create a hierarchy of directories. i.e., sub-directories inside the parent directory and so on.

For example,

import java.io.File;
import java.util.Scanner;
public class ABC {
  public static void main(String args[]) {
    System.out.println("Path? ");
    Scanner obj = new Scanner(System.in);
    String path = obj.next();
    System.out.println("Name of Directory? ");
    path = path + obj.next();
    File D = new File(path);
    boolean D1 = D.mkdirs();
    if (D1) {
      System.out.println("Folder is created successfully");
    } else {
      System.out.println("Error Found!");
    }
  }

Output:

Path? 
C:/
Name of Directory? 
TestDirectory/TestDirectory1
Folder is created successfully

Using mkdirs() method TestDirectory1 is created inside its parent directory TestDirectory. This can not be done using the mkdir() method.

Use the createDirectory() Function to Create Directories in Java

The Files.createDirectory() can also create a new directory in Java. A FileAlreadyExistsException is thrown if a file with the same name already exists.

For example,

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaCreateDirectory {
  public static void main(String[] args) throws IOException {
    String fileName = "C:\EbadTest\NewTest";

    Path path = Paths.get(fileName);

    if (!Files.exists(path)) {
      Files.createDirectory(path);
      System.out.println("New Directory created !   " + filename);
    } else {
      System.out.println("Directory already exists");
    }
  }
}

Output:

New Directory created !  C:\EbadTest\NewTest

If the directory already exists, then the code will give the following output.

Directory already exists

The above code will throw NoSuchFileException in case the hierarchy does not exist.

Use the createDirectories() Function to Create Directories in Java

The Files.createDirectories() can create a new directory if the parent directory does not exist. It can also make a hierarchy of directories. The createDirectories() method does not throw an exception if the directory already exists.

For example,

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaCreateDirectories {
  public static void main(String[] args) throws IOException {
    String fileName = "C:\NonExisting1\NonExisting2";
    Path path = Paths.get(fileName);
    Files.createDirectories(path);
    System.out.println("New Directory created !  " + fileName);
  }
}

Output:

New Directory created !  C:\NonExisting1\NonExisting2

Use the forceMkdir() Function to Create Directories in Java

The org.apache.commons.io.FileUtils package has the forceMkdir() function, creating directories and parent directories if required. It throws an exception if a file of the same name as the directory or the directory already exists.

For example,

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class JavaCreateDirectories {
  public static void main(String[] args) throws IOException {
    File directory = new File("C:\Test\Test1");
    FileUtils.forceMkdir(directory);
    System.out.println("New Directory created !");
  }
}

Output:

New Directory created ! 

Related Article - Java Directory