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.

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.

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

Advertisements

Ads
Share on Google+Share on Google+

Java Create Directory Example

Posted on: January 18, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Java Create Directory Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads