Java File mkdir() method example

HQ ¬Ľ Java Tutorial ¬Ľ ¬Ľ File ¬Ľ mkdir() method example
Java File mkdir() method example 2018-02-16T07:26:40+00:00 mkdir()


On this document we will be showing a java example on how to use the mkdir() method of File Class. This method creates the directory named by this abstract pathname.


  • SecurityException – If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method does not permit the named directory to be created.

Method Syntax

public boolean mkdir()

Method Argument

Data Type Parameter Description

Method Returns

This method returns boolean, true if and only if the directory was created; false otherwise.


Requires Java 1.0 and up

Java File mkdir() Example

Below is a java code demonstrates the use of mkdir() method of File class. The example presented might be simple however it shows the behaviour of the mkdir() method of File class. Basically we called this method and we put a check on the returned value to check if the file specified has been created successfully or not.

The example provided below shows that on the first run, the file has been created successfully while on the second run it failed. That is because the file is already created on the first run, thus succeeding run will definitely fail as describe on the earlier part of this document.

Sample Output

Below is the sample output when you run the above example.

java lang File mkdir() example output

Contact Info


Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.