In this post we will discuss the difference between File APIs two methods mkdir() and mkdirs().
mkdir() vs mkdirs()
mkdir() method will create folder only if the immediate parent folder is missing. If any of the parent the folder is missing the mkdir() method will return false.
mkdirs() method will create all the folders if all the folders are missing. If a folder is missing it will create the missing folder and then it will create the child folders.
package com.digitizedpost.files;
import java.io.File;
import java.io.IOException;
public class MakeDirectory {
public static void main(String[] args) {
try {
makeDirectory();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void makeDirectory() throws IOException {
File file1 = new File("C:\\Files\\text\\Spring Boot Tutorial.txt");
File file2 = new File("C:\\Files\\pdf\\Spring Boot Tutorial.pdf");
File file3 = new File("C:\\Files\\pdf\\temp\\Spring Boot Tutorial.pdf");
if (!file1.getParentFile().exists()) {
System.out.println("File 1. Parent Folder Not Exists");
file1.getParentFile().mkdir();
System.out.println("Creating new File : " + file1.getName());
file1.createNewFile();
}
if (!file2.getParentFile().exists()) {
System.out.println("File 2. Parent Folder Not Exists");
file2.getParentFile().mkdirs();
System.out.println("Creating new File : " + file1.getName());
file2.createNewFile();
}
if (!file3.getParentFile().exists()) {
System.out.println("File 3. Parent Folder Not Exists");
file3.getParentFile().mkdirs();
System.out.println("Creating new File : " + file1.getName());
file3.createNewFile();
}
}
}
Error
If the mkdir is used to create folders when too many folders are missing or any of the folders is missing other than the last folder it will result in the following error. When you try to create a new file after mkdir returns false, it will result in IOException. So you have to use mkdirs method if there are too many folders.
java.io.IOException: The system cannot find the path specified
File 1. Parent Folder Not Exists
Creating new File : Spring Boot Tutorial.txt
java.io.c: The system cannot find the path specified
at java.base/java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.base/java.io.File.createNewFile(File.java:1026)
at com.digitizedpost.files.MakeDirectory.makeDirectory(MakeDirectory.java:24)
at com.digitizedpost.files.MakeDirectory.main(MakeDirectory.java:9)
To avoid the above error test the mkdir with an if statement like below,
if(file1.getParentFile().mkdir()) {
System.out.println("Creating new File : " + file1.getName());
file1.createNewFile();
}
In simple terms, mkdir is used to create a folder (that too last folder in the path) and mkdirs is used to create folders. Hope this clears it up.
Lets's Practice
Suppose you have folders like folder1, folder2, folder3 and remove folder2 and see what happens with mkdir and mkdirs method. Test it by creating many folders and then removing a folder in the middle and removing only the last folder. Test these scenarios separately with mkdir or mkdirs method.
We have similar methods called createDirectory() and createDirectories() in NIO Files package and offer better capabilities than the mkdir and mkdirs IO File methods.