131

Can anyone tell me the difference between these two methods:

  • file.mkdir()
  • file.mkdirs()
Ahmed Maad
  • 127
  • 10
Krishna Kankal
  • 1,493
  • 2
  • 9
  • 4
  • 25
    Read the javadoc: [mkdir](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdir%28%29) vs [mkdirs](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#mkdirs%28%29) – assylias Mar 22 '12 at 10:11
  • 3
    The newer way to do it is using Files.createDirectories and Files.createDirectory static methods: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html – neves Feb 27 '18 at 18:26
  • You can get "File not supported" after using mkdir. – user7856586 Apr 05 '18 at 10:42
  • **Is there any performance differences between the two? _Especially when the parent directories already exist?_** – Joshua Pinter Oct 12 '19 at 20:15

3 Answers3

161

mkdirs() also creates parent directories in the path this File represents.

javadocs for 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.

javadocs for mkdir():

Creates the directory named by this abstract pathname.

Example:

File  f = new File("non_existing_dir/someDir");
System.out.println(f.mkdir());
System.out.println(f.mkdirs());

will yield false for the first [and no dir will be created], and true for the second, and you will have created non_existing_dir/someDir

amit
  • 166,614
  • 24
  • 210
  • 314
  • 8
    Because oracle is breaking links in the internet again: [mkdirs()](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#mkdirs%28%29) and [mkdir()](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#mkdir%28%29) – MyPasswordIsLasercats Jan 31 '14 at 16:07
  • @MyPasswordIsLasercats Thank you for letting me know. fixed. – amit Jan 31 '14 at 16:08
  • 1
    If the directory already exists, does `mkdir()` return `true` or `false`? The javadoc does not seem to cover this aspect. – Arun Mar 03 '15 at 18:55
  • @Arun, according to the Javadoc it returns "true if and only if the directory was created", which I suppose is ambiguous as to whether it was created by this call or earlier. [This answer](http://stackoverflow.com/a/12204054/894885) suggests the former. – Samuel Edwin Ward Mar 04 '15 at 19:51
  • mkdir and mkdirs return both false in my case -.-. It works if i use double backslash "\\" BUT: if i do ".toURI()" after that i receive: file:/Users/MyName/Desktop/%5Cnon_existing_dir%5CsomeDir/ and if i do ".getPath()" i receive "\non_existing_dir\someDir" and if i do ".getCanonicalPath()" i receive /Users/MyName/Desktop/\non_existing_dir\someDir – Aerox May 25 '15 at 11:47
  • @amit mkdirs works flawlessly! – Gaurav Jun 10 '19 at 15:00
62

mkdirs() will create the specified directory path in its entirety where mkdir() will only create the bottom most directory, failing if it can't find the parent directory of the directory it is trying to create.

In other words mkdir() is like mkdir and mkdirs() is like mkdir -p.

For example, imagine we have an empty /tmp directory. The following code

new File("/tmp/one/two/three").mkdirs();

would create the following directories:

  • /tmp/one
  • /tmp/one/two
  • /tmp/one/two/three

Where this code:

new File("/tmp/one/two/three").mkdir();

would not create any directories - as it wouldn't find /tmp/one/two - and would return false.

Alex Lockwood
  • 81,274
  • 37
  • 197
  • 245
Dave Webb
  • 179,733
  • 56
  • 298
  • 296
4
mkdir()

creates only one directory at a time, if it is parent that one only. other wise it can create the sub directory(if the specified path is existed only) and do not create any directories in between any two directories. so it can not create smultiple directories in one directory

mkdirs()

create the multiple directories(in between two directories also) at a time.

Durga Rao
  • 157
  • 2
  • 5
  • 19