Home Java Beginners Java Create Directory - Java Tutorial



Java Create Directory - Java Tutorial
Posted on: June 4, 2007 at 12:00 AM
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

Related Tags for Java Create Directory - Java Tutorial:
cfileclassfilesprocesspathdirectoryiosedsystemtracnameintthisaiabstractiecallwithprogramtoautomaticramicalpresentationnamesdirectoriesprocautoeilitrepresentationlsbstruseceinnocalasmnttrcaclesemdirautomaticallyallmeprorateexplaincreatingsystorsrectsoctordirectessbstatancestorracisirllomapreeaandactstrreprxpxissrocrithstabatiexictolsointeracticaicaplplaiprocesprndonomogronon


More Tutorials from this section

Ask Questions?    Discuss: Java Create Directory - Java Tutorial   View All Comments

Post your Comment


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

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.