Java Create Directory Example

Ads
 

Java Create Directory Example

To create a directory in java call the mkdir() method of java.io.File class. The mkdir() will create the single directry, to many directories in directory you need to call the mkdirs() mehtod.

To create a directory in java call the mkdir() method of java.io.File class. The mkdir() will create the single directry, to many directories in directory you need to call the mkdirs() mehtod.

Java Create Directory Example

To create a directory in java call the mkdir() method of java.io.File class. The mkdir() will create the single directry, to many directories in directory you need to call the mkdirs() mehtod.

An example of creating a directory in java is given below

SampleInterfaceImp.java

import java.io.*;
public class JavaWriteToAFileDirectoryExample{
	public static void main(String args[]){
	  try{
		
		  System.out.println("Please Enter Directory Name");
		  BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		  String directoryName=br.readLine();

		  // Create one directory
		  boolean success = (new File(directoryName)).mkdirs();
		  
		  if (success) {
			System.out.println("Directory: " + directoryName + " created");
		  }  
	  }catch (Exception e){//Catch exception if any
	  System.err.println("Error: " + e.getMessage());
	  }  
	}
}

Please run the application as given below,

F:\Core Java Examples>javac JavaWriteToAFileDirectoryExample.java

F:\Core Java Examples>java JavaWriteToAFileDirectoryExample
Please Enter Directory Name
directory1/dir2/dir3/dir4/
Directory: directory1/dir2/dir3/dir4/ created

Download Select Source Code

Ads

Ads