JAVA I/O - Creating a Directory

Guest Posts Paolo Nikko Nuñal

Java can create wonders when it comes to file handling and in this segment we are going to learn on how you can create directories using Java. These directories are useful if you have an application that manipulates files and folders. Creating a directory can be done easily with Java using the File utility method.

import java.io.File;

public class DemoCreateDirectory {
 public static void main(String args[]) {
 String directoryName = "/parent/child/baby";
 File directory = new File(directoryName);
 directory.mkdirs();
 }
}


From the example given, we have created String that will create the directory that we want to have. In our case, we have created three folders the "parent", "child" and "baby". In which, they are then passed to the directoryName to be treated as the directory to be created. After which we have created a new file object named directory in which it accepts one parameter for its constructor. In this case it asks for the directory path.

The mkdirs() method is the one responsible for creating the directory. You can check out the created directory in your drive C:/ path if you happen not to set a drive letter in creating a directory.