Create Directory Java Program

Sharing is healthy !!

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

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

Java create directory 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.

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

Now let’s see an example on create a directory in java or makedirectory method,

Example: Create Directory Java Program

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 directory in java program without using “if else” statement. Let’s see how to create a new directory in java,

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


make directories method

Make directories method will create all directories that are missing in the path which the file object represent.

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

In this example, assume “Java” folder 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.

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 directories are already created.


delete a directory

Now let us learn how to delete a directory which contains a lot of subdirectories and files.

Using file.delete method we can delete the directory. But the directory should be empty in order to delete it.

You can also read – Java Delete Directory

If the directory contains a lot of subdirectories and files then we have to use recursive delete in order to delete all subdirectories and files.

Let’s see an example,

import java.io.File;
import java.io.IOException;

public class DeleteDirectoryDemo
{
   private static final String SRC_FOLDER = "D:/Java/DirectoryExample";

   public static void main(String[] args) throws IOException
   {
      File directory = new File(SRC_FOLDER);
      // checking if directory exists
      if(!directory.exists())
      {
         System.out.println("Directory doesn't exist!!");
      }
      else
      {
         DeleteDirectoryDemo del = new DeleteDirectoryDemo();    
         del.directoryDelete(directory);
      }
   }

   public void directoryDelete(File fl) throws IOException
   {
      if(fl.isDirectory())
      {
         // if directory is empty, delete it
         if(fl.list().length == 0)
         {
            emptyDirectory(fl);
         }
         else
         {
            // listing all directories
            File file[] = fl.listFiles();
            for(File fileDelete : file)
            {
               // recursive delete
               directoryDelete(fileDelete);
            }
            if(fl.list().length == 0)
            {
               emptyDirectory(fl);
            }
         }
      }
      else
      {
         fl.delete();
         System.out.println("Files are deleted : " + fl.getAbsolutePath());
      }
   }
   
   private void emptyDirectory(File file)
   {
      file.delete();
      System.out.println("Directory deleted : " + file.getAbsolutePath());
   }
}

conclusion

That’s it guys. This is all about java create directory using file class and an example on create directory java program.

I hope you have understood how to create directory in java. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Sharing is healthy !!

You May Also Like