Create Directory Java Program

Sharing is healthy !!

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.

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 it is created successfully then this method returns true. If it is not created successfully then it will return false.

Now let’s see an example on makedirectory method,

Example: Create Directory Java Program

Create Directory Java Program

Also Read – Create File 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 above program without using “if else” statement. Let’s see an example,

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 till “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 an example now,

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.

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 directories using file class and an example on create directory java program.

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

Do share this post if you like.

Sharing is healthy !!