Difference between mkdir() and mkdirs() in java for java.io.File

Krishna Kankal picture Krishna Kankal · Mar 22, 2012 · Viewed 140.9k times · Source

Can anyone tell me the difference between these two methods:

  • file.mkdir()
  • file.mkdirs()

Answer

amit picture amit · Mar 22, 2012

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