Java File mkdirs() method example

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

java.io.File mkdirs()

Description

On this document we will be showing a java example on how to use the mkdirs() method of File Class. This method creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

Throws:

  • SecurityException – If a security manager exists and its SecurityManager.checkRead(java.lang.String) method does not permit verification of the existence of the named directory and all necessary parent directories; or if the SecurityManager.checkWrite(java.lang.String) method does not permit the named directory and all necessary parent directories to be created.

Method Syntax

public boolean mkdirs()

Method Argument

Data Type Parameter Description
N/A N/A N/A

Method Returns

This method returns boolean, true if and only if the directory was created, along with all necessary parent directories; false otherwise.

Compatibility

Requires Java 1.0 and up

Java File mkdirs() Example

Below is a java code demonstrates the use of mkdirs() method of File class. The example presented might be simple however it shows the behaviour of the mkdirs() method of File class. Basically we called this method and we put a check on the returned value to check if the pathname 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 directory structure 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 mkdirs() example output

Contact Info

Singapore

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.

Close