Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Java Create Directory - Java Tutorial

 
 

Share on Google+Share on Google+

Java Create Directory - Java Tutorial

Advertisement
In the section of Java Tutorial you will learn how to create directory using java program.

Java Create Directory - Java Tutorial

     

In the section of Java Tutorial you will learn how to create directory using java program. This program also explains the process of creating all non-existent ancestor directories automatically. We will use the class File class to crate the directory.

Class File
The File class an abstract representation of file and directory pathnames. File class is used to interact with the files system.


Here is the code for creating directory and all non-existing ancestor directories:

import java.io.*;
class CreateDirectory 
{
 public static void main(String args[])
  {
  try{
  String strDirectoy =
"test";
  String strManyDirectories="dir1/dir2/dir3";

  // Create one directory
  boolean success = (
 
new File(strDirectoy)).mkdir();
  if (success) {
  System.out.println("Directory: " 
   + strDirectoy + 
" created");
  }  
  // Create multiple directories
  success = (new File(strManyDirectories)).mkdirs();
  if (success) {
  System.out.println("Directories: " 
   + strManyDirectories + 
" created");
  }

  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Download the code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 4, 2007

Related Tutorials

Discuss: Java Create Directory - Java Tutorial   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:12
Sangamesha
January 10, 2012
java...

Ya dis is good one....
Deepak Mishra
February 4, 2012
Excellent Matter On Java Program

Excellent progranm and example
Thomas mcNaul
April 11, 2012
creating java directory

I cant figure out how to create A java directory. please reply with details.
aaron
June 15, 2012
math

hi
aaron
June 15, 2012
math

awesome website
easton
July 13, 2012
java

is java something you need
sakthilakshmi
August 29, 2012
Java

Thank you so much for coding.You are doing great job for the beginners. Thanks a lot !
hi
September 12, 2012
hi

it is cool
Nick Kaarstens
September 14, 2012
Virtual File System

Hi, I'm trying to implement a virtual file system in Java (making directories, files, deleting, listing etc). Not sure how to start with it. Any suggestions? Cheers
DMCA.com