Learn to create a new directory in a specified path using Java IO and NIO classes. We will go through the methods that can and cannot create the parent directories if they do not exist.
1. Overview
We will create a new directory at the location c:/temp/data/archive
. Notice the data
is the parent directory of archive
. In runtime, it is possible that data
directory may not exist when we try to create archive
directory.
We will learn to create the archive
directory in such a way:
- if
data
directory does not exist then create it - if
data
directory does not exist then throw an exception
2. Using NIO Files
The Files
class has two methods:
2.1. Files.createDirectory()
- The
createDirectory()
creates the new directory if all the parent directories exist. - The check for the existence of the file and the creation of the directory if it does not exist are a single atomic operation.
- If a file or directory of a similar name already exists in the specified path then
FileAlreadyExistsException
exception is thrown. - In case any of the parent directories do not exist the
IOException
is thrown.
Path path = Paths.get("c:/temp/data/archive");
if (!Files.exists(path)) {
Files.createDirectory(path);
}
2.2. Files.createDirectories()
- The
createDirectory()
creates the new directory by creating all nonexistent parent directories first. - It does not throw an exception if a directory with the same name and path already exists. In this case, nothing happens.
- If a file of same name already exists in the specified path then
FileAlreadyExistsException
exception is thrown. - It is not an atomic operation. So it is entirely possible that this operation may create some of the parent directories and later fail to complete for some reason.
Path path = Paths.get("c:/temp/data/archive");
Files.createDirectories(path);
3. Using IO File
The File
class has also two similar methods:
3.1. File.mkdir()
The mkdir()
method creates a new directory in the specified path. This method returns true
if and only if the directory was created; otherwise false
is returned.
In case of write permission issue, it may throw SecurityException
.
File newDir = new File("c:/temp/data/archive");
if(!newDir.exists()) {
boolean isDirectoryCreated = newDir.mkdir();
}
As a best practice, we should always verify if the directory exists with the specified name after this operation. To do so, we can use newDir.exists()
again after this operation.
3.2. File.mkdirs()
The mkdirs()
method creates the directory of the specified name including any necessary but nonexistent parent directories.
- Similar to createDirectories(), this operation may fail after creating some of the parent directories.
- It returns
true
if and only if the directory was created, along with all necessary parent directories;false
otherwise.
File newDir = new File("c:/temp/data/archive");
boolean isDirectoryCreated = newDir.mkdirs();
4. Conclusion
In this Java tutorial, we learned to create a new single directory as well as a nested directory along with its all parent directories.
Use of NIO Files
is recommended in comparison to old IO File
class.
Happy Learning !!
Leave a Reply