Java Create Folder Sample Code

The File.mkdir() method of the Java File class lets you easily create new folders or directories as long as you have the proper permission to do so.


Java Create Folder Source Code

package com.jcd.java.tutorials;

import java.io.File;

public class JavaCreateFolder 
{

	public static void main(String[] args) 
	{
		File jcdBaseFolder = new File("C:\\Users\\Admin\\Conf");
		
		//Does teh base folder exist?
		if(jcdBaseFolder.exists()) 
		{
			System.out.println(jcdBaseFolder+" Exists!");
		}
		
		//Create new sub folder
		File jcdSubFolder = new File(jcdBaseFolder+File.separator+"JavaCodeDepot");
		
		Boolean folderCreateStatus = jcdSubFolder.mkdir();
		
		//Check to see if sub folder was created
		if(folderCreateStatus)
		{
			System.out.println(jcdSubFolder+" Created!");
		}
		else
		{
			System.out.println(jcdSubFolder+" Already Exists!");
		}
	}
}


Java Create Folder Output

C:\Users\Admin\Conf Exists!
C:\Users\Admin\Conf\JavaCodeDepot Created!


After running the sample code, you can see the newly created sub folder called JavaCodeDepot.


Add a Comment

Your email address will not be published. Required fields are marked *