Mkdirs () returns a value for existing directories

File.mkdirs JavaDocs:

public boolean mkdirs ()

Creates a directory with the name of this abstract path, including any necessary but non-existent parent directories. Please note that if this operation failed, it may have been able to create some of the required parent directories.

Returns: true if and only if the directory was created along with all the necessary parent directories; false otherwise

My question is: returns mkdirs () false if some of the directories he wanted to create already existed? Or does it just return true if it succeeds in creating the entire path to the file, regardless of whether any directories exist?

+7
java file
source share
1 answer

It returns false.

From java doc: - true if the directory was created, false on error, or if the directory already exists.

You should do something like this:

if (file.mkdirs()) { System.out.format("Directory %s has been created.", file.getAbsolutePath()); } else if (file.isDirectory()) { System.out.format("Directory %s has already been created.", file.getAbsolutePath()); } else { System.out.format("Directory %s could not be created.", file.getAbsolutePath()); } 
+8
source share

All Articles