In order to create a directory (folder) using Java code we can make use of the createDirectory() method from the Files class that was introduced in Java 7.
Example:import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaCreateDirectoryExample {
public static void main(String[] args) {
String dirName = "myDir";
String pathStr = "/Users/code2care/my-projects-22/java-examples/";
Path dirPathAndName = Paths.get(pathStr+dirName);
try {
Path path = Files.createDirectory(dirPathAndName);
System.out.println("Directory created: " + path);
} catch (IOException e) {
System.out.println("Error occurred while creating directory!...");
e.printStackTrace();
}
}
}
Output:
Directory created: /Users/code2care/my-projects-22/java-examples/myDir

Provide Feedback For This Article
We take your feedback seriously and use it to improve our content. Thank you for helping us serve you better!
😊 Thanks for your time, your feedback has been registered!
Comments & Discussion
Facing issues? Have questions? Post them here! We're happy to help!