There are several ways to create a directory in Java upon the java version you are using, In this tutorial, we are going to see the ways to create a directory using Java.
How to create directory in Java:
Java 7 introduced an incredible nio package, it helps us do a lot of io operations like reading the present working directory in a better way, lets try to create a directory using nio.
1.Create Directory Java NIO:
With Java 7 nio, you can use Files.createDirectory()
to create a new directory. It takes a Path as a parameter and creates a directory under the specified Path, it also takes an optional array of FileAttrubite parameters (key values pairs of file attributes), these attributes to be set automatically while creating the directory.
- UnsupportedOperationException – if the FileAttribute array contains an attribute that cannot be set atomically when creating the directory.
- FileAlreadyExistsException – if a specified directory path has already existed.
- IOException – if an I/O error occurs or the parent directory does not exist
public class JavaCreateDirectory {
public static void main(String[] args) throws Exception {
Path path = Paths.get("/Users/chandra/Work/hello/");
Files.createDirectory(path);
}
}
1.1 Create Directories Java NIO:
Files.createDirectories()
creates a directory by creating all nonexistent parent directories first. Unlike Files.createDirectory() it doesn’t throw an exception if the directory could not be created because it already exists.
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaCreateDirectory {
public static void main(String[] args) throws Exception {
Path path = Paths.get("/Users/chandra/Work/hello/parent1/parent2/");
Files.createDirectories(path);
}
}
2. Create a Directory Apache Commons:
Apache Commons is a great library for handling io operations, it provides a lot of useful methods.
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.7</version>
</dependency>
FileUtils.forceMkdir()
it makes a directory, including any necessary but nonexistent parent directories. If a file already exists with a specified name but it is not a directory then an IOException is thrown.
public class JavaCreateDirectory {
public static void main(String[] args) throws Exception {
FileUtils.forceMkdir("/path/directory");
}
}
3. Create a Directory Legacy IO:
The legacy java users can use the File object to create directory(s); java.io.File class having the same methods mkdir() and mkdirs() to create a directory or directories.
The mkdir() and mkdirs() methods returns boolean –true
if and only if the directory was created; false
otherwise
public class JavaCreateDirectory {
public static void main(String[] args) throws Exception {
File file = new File("/Users/chandra/dir1/dir2/dir2/");
if (file.mkdirs()) {
System.out.println("Directories are created!");
} else {
System.out.println("Failed to create directories");
}
}
}
References:
Happy Learning 🙂