last modified January 27, 2024
In Java create directory tutorial, we show how to create a directory in Java. We also show how to set directory permissions on POSIX systems.
A computer directory is an organizational file system structrure that contains files and optionally other directories.
The java.nio.file.Files class consists of static methods
that operate on files, directories, or other types of files.
The Files.createDirectory creates a new directory.
If a file already exists, a FileAlreadyExistsException is thrown.
package com.zetcode;
import java.io.IOException;
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 IOException {
String fileName = "/home/janbodnar/tmp/newdir";
Path path = Paths.get(fileName);
if (!Files.exists(path)) {
Files.createDirectory(path);
System.out.println("Directory created");
} else {
System.out.println("Directory already exists");
}
}
}
The example creates a new directory with Files.createDirectory.
String fileName = "/home/janbodnar/tmp/newdir"; Path path = Paths.get(fileName);
A Path is created from the file name. A Path is a Java object
used to locate a file in a file system.
if (!Files.exists(path)) {
We first check if the directory does not already exist with Files.exists.
Files.createDirectory(path);
The directory is created with Files.createDirectory. The method takes
a path object as a parameter.
The Files.createDirectories creates a new directory; if the parent directories
do not exist, they are created as well. The method does not thrown an exception if the directory
already exist.
package com.zetcode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaCreateDirectories {
public static void main(String[] args) throws IOException {
String fileName = "/home/janbodnar/docs/memos";
Path path = Paths.get(fileName);
Files.createDirectories(path);
}
}
The example creates a new directory with Files.createDirectories.
With PosixFilePermissions, we can create a new directory and set
its permissions. Note that this class cannot be used for Windows systems.
package com.zetcode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
public class JavaCreateDirectoryWithPermissions {
public static void main(String[] args) throws IOException {
String fileName = "/home/janbodnar/tmp/newdir";
Path mypath = Paths.get(fileName);
if (!Files.exists(mypath)) {
Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rwxr--r--");
FileAttribute<Set<PosixFilePermission>> fileAttributes = PosixFilePermissions.asFileAttribute(permissions);
Files.createDirectory(mypath, fileAttributes);
System.out.println("Directory created");
} else {
System.out.println("Directory already exists");
}
}
}
The example creates a new directory with the specified permissions.
Java Creating and Reading Directories - tutorial
In this article we have shown how to create directories in Java.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Java tutorials.