How to create directory in java

In order to create directory in java use below codes.

1. JDK 1.5

file.mkdir();

2. JDK1.7 after

 

Code Example of Java Create Directory

package JavaIOExample;

import java.io.File;
import java.util.Scanner;

/*
 * Here we will learn to create Directory only.
 */
public class CreateDirectory {

	public static void main(String args[]) {

		// Here we are assuming that D:\Gautam\testing folder exists and user
		// has privilege to create directory in that folder
		File file = new File("D:\JBT\testing\first");

		boolean b = false;

		/*
		 * exists() method tests whether the file or directory denoted by this
		 * abstract pathname exists or not accordingly it will return TRUE /
		 * FALSE.
		 */

		if (!file.exists()) {
			/*
			 * mkdir() method creates the directory mentioned by this abstract
			 * pathname excluding any necessary but nonexistent parent
			 * directories.
			 * 
			 * Accordingly it will return TRUE or FALSE if directory created
			 * successfully or not.
			 */
			b = file.mkdir();
		}
		if (b)
			System.out.println("Directory successfully created");
		else
			System.out.println("Failed to create directory");
	}
}

 

By | 2012-09-21T19:40:18+00:00 September 21st, 2012|Code Base|0 Comments

About the Author:

Leave A Comment