In Java, we can use the NIO Files.createDirectory to create a directory or Files.createDirectories to create a directory including all nonexistent parent directories.
try {
Path path = Paths.get("/home/favtuts/a/b/c/");
//java.nio.file.Files;
Files.createDirectories(path);
System.out.println("Directory is created!");
} catch (IOException e) {
System.err.println("Failed to create directory!" + e.getMessage());
}
1. Create Directory – Java NIO
1.1 We can use Files.createDirectory to create a directory.
- If the parent directories not exist, throws
NoSuchFileException. - If the directory exists, throws
FileAlreadyExistsException. - If IO errors, throws
IOException.
Path path = Paths.get("/home/favtuts/test2/");
Files.createDirectory(path);
1.2 We can use Files.createDirectories creates a directory including all nonexistent parent directories.
- If the parent directories not exist, create it first.
- If the directory exists, no exception thrown.
- If IO errors, throws
IOException
Path path = Paths.get("/home/favtuts/test2/test3/test4/");
Files.createDirectories(path);
1.3 This example uses Files.createDirectories to create a directory /test4/ including all nonexistent parent directories /test2/test3/.
DirectoryCreate1.java
package com.favtuts.io.directory;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DirectoryCreate {
public static void main(String[] args) {
String dir = "/home/tvt/workspace/favtuts/test2/test3/test4/";
createDirectoriesNIO(dir);
}
public static void createDirectoriesNIO(String dir) {
try {
Path path = Paths.get(dir);
Files.createDirectories(path);
System.out.println("Directory is created!");
//Files.createDirectory(path);
} catch (IOException e) {
System.err.println("Failed to create directory!" + e.getMessage());
}
}
}
2. Create Directory – Legacy IO
For legacy IO java.io.File, the similar methods are file.mkdir() to create a directory, and file.mkdirs() to create a directory including all nonexistent parent directories.
Both file.mkdir() and file.mkdirs() returns a boolean, true if success to create the directory, fail otherwise, no exception thrown.
DirectoryCreate2.java
package com.favtuts.io.directory;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class DirectoryCreate {
public static void main(String[] args) {
String dir = "/home/tvt/workspace/favtuts/test2/test3/test4/";
createDirectoriesLegacy(dir);
}
public static void createDirectoriesLegacy(String dir) {
File file = new File(dir);
// true if the directory was created, false otherwise
if (file.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
}
In legacy IO, the lack of exception thrown in creating directory makes developers very hard to debug or understand why we cannot create a directory, and this is one of the reasons Java releases a new java.nio.Files to throw a proper exception.
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/directory