Create Directory Java Program

Hey guys!! Welcome to flower brackets blog. In this post we are going to learn a classic example of create directory java program.

create directory java program

Here File class contains two methods make directory(mkdir) and make directories(mkdirs). Using these two methods we can create directories if it does not exist.

mkdir or Make directory method creates single directory if it does not exist.

File directory = new File("D:/Java/DirectoryExample");
boolean isCreated = directory.mkdir();

In this example if “Java” folder is already there in the D drive, then “makedirectory” method will create “DirectoryExample” directory under this “Java” folder.

Also Read – Java List Directory

And if folder is created successfully then this method returns true. If folder is not created successfully then it will return false.

Now let’s see an example on how to create a directory in java,

Example: create directory java program

import java.io.*;

public class CreateDirectory
{
   public static void main(String[] args)
   {
      File directory = new File("D:/Java/DirectoryExample");
      boolean isCreated = directory.mkdir();
      if(isCreated)
      {
         System.out.println("Directory created");
      }
      else
      {
         System.out.println("Directory not created");
      }
   }
}

Output:

Directory created


You can write java program to create directory without using “if else” statement. Let’s see java code to create a new directory,

import java.io.File;

public class CreateDirectoryDemo
{
   public static void main(String[] args)
   {
      File directory = new File("D:/Java/DirectoryExample");      
      boolean isCreated = directory.mkdir();   
      System.out.println(directory.getAbsolutePath() + " directory created? = " + isCreated);
   }
}

Output:

D:/Java/DirectoryExample directory created? = true


How to create directories in java using make directories method

Make directories method in java create more than one directories together at a time.

File directory = new File("D:/Java/DirectoryExample/file/src");
boolean isCreated = directory.mkdirs();

In this example, assume “Java” folder is already present in D drive then, what the make directories method will do is, it will create “DirectoryExample” under “java” folder, “file” folder under “DirectoryExample” folder and source folder under “file” folder.

Also Read – Java Rename File

Let’s see create a directory in java example,

import java.io.File;

public class CreateDirectoryDemo
{
   public static void main(String[] args)
   {
      File directory = new File("D:/Java/DirectoryExample/file/src");
      boolean isCreated = directory.mkdirs();     
      System.out.println(directory.getAbsolutePath() + " directory created ? = " + isCreated);
   }
}

Output:

D:\Java\DirectoryExample\file\src directory created ? = true

Again if you run this program it will return false. Because directory is already created.

NOTE : make directories method(mkdirs) and make directory method(mkdir) both returns boolean value.


Java 7 create directory

In java 7 we can create a 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\\Sub-Subdirectory1");
      // 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();
         }
      }
   }
}


Conclusion

That’s it guys. This is all about how to create directory in java.

I hope you have understood the concept. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Related Posts