Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

create
directory
files
java

How to create a directory using Java

Educative Answers Team

In Java, the mkdir() function is used to create a new directory. This method takes the abstract pathname as a parameter and is defined in the Java File class.

mkdir() returns true if the directory is created successfully; else, it returns false​.

Syntax

Take​ a look at the function signature of mkdir() below:

svg viewer

Code

The code snippet below illustrates the usage of the mkdir() function to create a new directory:

import java.io.*; 

class CreateDirectory { 

	public static void main(String args[]) 
	{ 
		// specify an abstract pathname in the File object 
		File f = new File("D:\\Educative"); 

		// check if the directory can be created 
		// using the specified path name 
		if (f.mkdir() == true) { 
			System.out.println("Directory has been created successfully"); 
		} 
		else { 
			System.out.println("Directory cannot be created"); 
		} 
	} 
} 

The code above may not run in an online IDE. To compile the code, copy the above code​ into an offline IDE and set an appropriate path.

RELATED TAGS

create
directory
files
java
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring