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

Suggested Reading List

References