How to create directory in java

Let’s learn how to create directory in java.

How to create directory in java

How to create directories in java is most frequently asked java interview question.

There are two methods to create directory. They are mkdir() and mkdirs() of class File which returns boolean value.

how to create directory in java

mkdir() method create single directory.

File directory = new File(“D:\\DirectoryExample”);

Also read – java create file

Now let’s see file mkdir() method in java example.

import java.io.*;
public class CreateDirectory
{
   public static void main(String[] args)
   {
      File directory = new File("D:\\DirectoryExample");
      if(!directory.exists())
      {
         if(directory.mkdir())
         {
            System.out.println("Directory is created");
         }
         else
         {
            System.out.println("Directory not created");
         }
      }
   }
}


Output:

Directory created


mkdirs() method creates directories in java and its sub-directories together.

File directory = new File(“D:\\DirectoryExample\\subdirectory1\\subdirectory2”);

Here’s the file mkdirs() method in java example.

import java.io.File;
public class CreateDirectoryDemo
{
   public static void main(String[] args)
   {
      File directory = new File("D:\\DirectoryExample\\subdirectory1\\subdirectory2");
      if(!directory.exists())
      {
         if(directory.mkdirs())
         {
            System.out.println("Sub directories created");
         }
         else
         {
            System.out.println("Sub directories not created");
         }
      }
   }
}


Output:

Sub directories created

NOTE: make directories method(mkdirs) and make directory method(mkdir) in java both return boolean value to show program status.


Java NIO

In java 7 we can create directory using java nio packages as shown in below example.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class NioDirectoryExample
{
   public static void main(String[] args)
   {
      Path path = Paths.get("D:\\Directory1\\Subdirectory1\\SubSubdirectory2");
      // this will check if directory exists?
      if(!Files.exists(path))
      {
         try
         {
            Files.createDirectories(path);
         }
         catch(IOException ioe)
         {
            // if create directory fails to execute
            ioe.printStackTrace();
         }
      }
   }
}



java code to create folder and subfolders

Here’s the code to create new directory and sub-directories.

import java.io.File;
public class CreateFolderAndSubfolders 
{
   public static void main(String[] args) 
   {
      // create File object and pass folder or directory name
      File fl = new File("b:\New Folder");
      // exists() method tests whether file or directory exists
      if(!fl.exists()) 
      {
         // mkdir() creates directory
         if(fl.mkdir()) 
         {
            System.out.println("New Folder created.");
         }
         else 
         {
            System.out.println("Something went wrong. Try again!!");
         }
      } 
      else 
      {
         System.out.println("File already exists.");
      }
      // create File object
      File subFolder = new File("b:\Directory\subfolder1\subfolder2");
      // mkdirs() method creates directories
      if(subFolder.mkdirs()) 
      {
         System.out.println("Structure folder and subfolder created.");
      }
      else
      {
         System.out.println("Something went wrong. Try again!!");
      }
   }
}


Output:

New Folder created.
Structure folder and subfolder created.


how to check if a directory exists in java

To check if directory exists or not java.io.File.exists() method is used.

File.exists() method returns true if and only if the file or directory denoted by this abstract pathname exists; false otherwise.

Let’s see an example on java check if directory exists.

import java.io.File;
public class CheckIfExists 
{
   public static void main(String[] args) 
   {
      try 
      {
         File fl = new File("b:/JavaExamples/CheckIfExists.txt");
         fl.createNewFile();
         System.out.println(fl.exists());
      }
      catch(Exception ex) 
      {
         ex.printStackTrace();
      }
   }
}


Output:

true


create directory with permissions

To create directory with permissions use PosixFilePermissions.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
public class CreateDirectoryPermissions 
{
   public static void main(String[] args) throws IOException 
   {
      String strFile = "/javaexamples/sachin/dir/newDir";
      Path path = Paths.get(strFile);
      if(!Files.exists(path))
      {
         Set<PosixFilePermission> permission = PosixFilePermissions.fromString("rwxr--r--");
         FileAttribute<Set<PosixFilePermission>> file = PosixFilePermissions.asFileAttribute(permission);
         Files.createDirectory(path, file);
         System.out.println("Directory created");
      }
      else 
      {
         System.out.println("Oops!!Directory already exists");
      }
   }
}