Java createDirectory() and createDirectories() methods are present in NIO (New I/O) Files package. Let's discuss the difference between createDirectory and createDirectories method in this post.
Both createDirectory and createDirectories methods return's the file Path
createDirectory
createDirectory method creates only a single folder. For example, if you want to create folder2 and if parent exists (i.e., folder1) it will create the folder2.
If the parent folder does not exists it will throw NoSuchFileException
java.nio.file.NoSuchFileException: C:\Folder1\Folder2
private static void createDirectory() throws IOException {
Path filePath = Paths.get("C:\\Files\\");
String fileName = "Java Tutorial.doc";
if (!Files.exists(filePath)) {
System.out.println("Folder Not Exists");
System.out.println("Creating new Folder : " + Files.createDirectory(filePath));
} else {
System.out.println("Root Path -> " + filePath.getRoot());
System.out.println("Folder Exists");
//create new file here
}
}
Output
Creating New Directory
Folder Not Exists
Creating new Folder : C:\Files
When a folder already exists
Root Path -> C:\
Folder Exists
When a folder already exists one should check the code with Files.exists() method in the conditional statement and then call the Files.createDirectory() method, otherwise createDirectory() method will throw the FileAlreadyExistsException.
java.nio.file.FileAlreadyExistsException: C:\Files
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:87)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
at java.base/sun.nio.fs.WindowsFileSystemProvider.createDirectory(WindowsFileSystemProvider.java:506)
at java.base/java.nio.file.Files.createDirectory(Files.java:694)
at com.digitizedpost.files.CreateDirectory.createDirectory(CreateDirectory.java:26)
at com.digitizedpost.files.CreateDirectory.main(CreateDirectory.java:14)
createDirectories
createDirectories method creates all the directories including the parent directory
When a folder already exists it doesn't throw any exception like the createDirectory method.
If the folder exists we are going to create a new file in the else statement. You can create a new file or copy another file to the existing folder. Here we have written a sample text to the file.
package com.digitizedpost.files;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class CreateDirectory {
public static void main(String[] args) {
try {
//createDirectory();
createDirectories();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void createDirectory() throws IOException {
Path filePath = Paths.get("C:\\Files\\");
String fileName = "Java Tutorial.doc";
if (!Files.exists(filePath)) {
System.out.println("Folder Not Exists");
System.out.println("Creating new Folder : " + Files.createDirectory(filePath));
} else {
System.out.println("Root Path -> " + filePath.getRoot());
System.out.println("Folder Exists");
//create new file here
}
}
private static void createDirectories() throws IOException {
Path filePath = Paths.get("C:\\Files\\folder1\\folder2\\folder3");
String fileName = "Java Tutorial.doc";
if (!Files.exists(filePath)) {
System.out.println("Folder Not Exists");
System.out.println("Creating new Folder : " + Files.createDirectories(filePath));
} else {
System.out.println(filePath.getParent());
System.out.println("Folder Exists");
File file = new File(filePath + "/" + fileName);
try {
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Sample Text");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
In our last post, we have discussed the mkdir vs mkdirs method difference. Those methods use File IO API and you can compare the createDirectory and createDirectories method program with mkdir and mkdirs methods to see the difference.