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:

[root@mypc]# java CreateDirectory
Folder newDir created.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.
Get the Free Newsletter!
Subscribe to Developer Insider for top news, trends & analysis
This email address is invalid.

Latest Posts

Related Stories