Java Create Directory

The following example will show how to Create Directory in Java. Though this can be created by using mkdir() also, the following program has used file object in File class.

Java Create Directory

The following example will show how to Create Directory in Java. Though this can be created by using mkdir() also, the following program has used file object in File class.

Java Create Directory

The following example will show how to Create Directory in Java. Though this can be created by using mkdir() also, the following program has used file object in File class.

'try' and 'catch' are used for exception during execution of the program.

For this we cerate a file object in File class which will create a new directory "Roseindia" in C drive of your computer. If the directory already exists the dierctory won't be created and an output "Directory is already created" will be displayed.

Example of Java Create Directory:

import java.io.File;
import java.io.IOException;

public class CreateFileLocation {
	public static void main(String[] t) {
		File file = new File("c:/Roseindia");
		try {
			if (file.createNewFile()) {
				System.out.println("Directory is created");
			} else {
				System.out.println("Directory is already created");
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}

	}
}