23

File.mkdirs JavaDocs:

public boolean mkdirs()

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.

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

My question is: Does mkdirs() return false if some of the directories it wanted to create already existed? Or does it just return true if it was successful in creating the entire path for the File, whether or not some directories already existed?

Community
  • 1
  • 1
Davio
  • 4,609
  • 2
  • 31
  • 58
  • 1
    A path is hierarchical like `/a/b/c/d` - there will always be a part that exists already, and it can't fail just in between because that would result in a different part like `/a/b/d/`. So "true" is only then returned when it had to create (at least) that last path part (`d`) and succeeded, the parts before must exist in logical consequence. – zapl Nov 27 '14 at 11:46

1 Answers1

22

It returns false.

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

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());
}
chrizdekok
  • 327
  • 1
  • 4
  • The question is about the parent directories, not the target directory itself. – WilQu Nov 27 '14 at 12:28
  • 1
    This is not a correct statement. The documentation states: "true if and only if the directory was created, along with all necessary parent directories; false otherwise" which doesn't have the same meaning as your statement - it doesn't claim it will return false if directories or full path exists. – Predrag Manojlovic Sep 10 '19 at 07:14
  • @PredragManojlovic It's exactly the same thing. If the directory already exists, then it can't be "created", therefore the method returns false as per the docs. – Adam Burley May 16 '22 at 16:56
  • @AdamBurley You are missing the point. Generally, if the path is /a/b/c/d it is unclear in the documentation what should we expect as result in case /a/b exists and it creates c/d. Even more unclear is if /a/b/c/d exists. What shall we expect as result? IF /a/b/c/d/ exists as a full path returning true is ok as the folder is generally there but it is also ok to return false if it was not created. So, the documentation is not clear. Means, that a programmer has to be an additional method to check if the full path already exists instead of calling mkdirs() anyways – Predrag Manojlovic May 16 '22 at 21:39
  • @PredragManojlovic "IF /a/b/c/d/ exists as a full path returning true is ok as the folder is generally there" - not correct, this is exactly what I was trying to say. returning true is not ok because it's not true that "the directory was created, along with all necessary parent directories" as per the documentation. – Adam Burley May 16 '22 at 22:03