JavaHow to Create a New Directory Folder in Java

How to Create a New Directory Folder in Java

In this quick Java tutorial, we are going to showcase how to create a new directory folder using Java. We will be relying on the Files.createDirectory method to create our directory folder. Feel free to copy the code below into your Integrated Development Environment (IDE) or code editor to follow along:

*/

import java.io.*;
import java.nio.file.*;

public class CreateDirectory
{
	public static void main(String args[])
	{
		CreateDirectory createDirectory = new CreateDirectory();
		createDirectory.proceed();
	}
	
	private void proceed()
	{
		final String NEW_DIR_NAME = "newDir";
		try
		{
			Files.createDirectory(FileSystems.getDefault().getPath(NEW_DIR_NAME));
			System.out.println("Folder " + NEW_DIR_NAME + " created.");
			
		}
		catch(IOException e)
        {
            e.printStackTrace();
        } 	
	}
	
}

/*

This Java code results in the following output when you run it in your development environment:

[[email protected]]# java CreateDirectory
Folder newDir created.

Latest Posts

Related Stories