Home > Tutorial > Java Create Folder Sample Code

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.


Seymour
Posted by

Seymour

I have been a software developer for twenty years now focusing on writing high quality Java applications. If you are in a programming field, you know that technology is constantly changing. You have to keep current or you will get left behind!

You may also like...

Leave a Reply

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

Copyright © 2018 JavaCodeDepot