Write a java program to Create directory or folder in particular drive


This code demonstrates how to create a directory using the File class in Java. The program creates a directory named "Sample" in the "D:" drive of the computer. Here is a step-by-step explanation of the code:

  • The program creates a new File object named dir and initializes it with the path of the directory to be created. In this case, the directory name is "Sample" and the path is "D:/Sample".
  • The mkdir() method of the File class is called on the dir object to create the directory. The mkdir() method returns a boolean value indicating whether the directory was created successfully or not.
  • If the directory was created successfully, the program prints the message "Directory Successfully Created..". Otherwise, it prints the message "Directory was Not Created Successfully..".

Overall, this code is a simple example of how to create a directory in Java using the File class.

Source Code

import java.io.*;
public class Create_Directory
{
	public static void main(String[] args)
	{
		File dir = new File("D:/Sample");
		boolean isDirectoryCreated = dir.mkdir();
		if (isDirectoryCreated)
		{
			System.out.println("Directory Successfully Created.." );
		}
		else
		{
			System.out.println("Directory was Not Created Successfully..");
		}
	}
}

Output

Directory Successfully Created..