tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > IO > Create Directoroy

Create Directoroy 

The following example shows how to create a simple top level directory and nested child directories.

File Name  :  
com/bethecoder/tutorials/io/CreateDirectory.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.io;

import java.io.File;

public class CreateDirectory {

  /**
   @param args
   */
  public static void main(String[] args) {

    /**
     * mkdir - creates directory if the parent directory/drive exists
     * return TRUE if the directory is created successfully
     * FALSE if the directory already exists
     */
    File newDir = new File("C:\\NEW");
    System.out.println(newDir + " created successfully : " + newDir.mkdir());
    
    /**
     * mkdir cannot create nested sub directories.
     * for that we need to use mkdirs.
     */
    newDir = new File("C:\\NEW2\\NEW2_SUB\\NEW2_SUB_SUB");
    System.out.println(newDir + " created successfully : " + newDir.mkdir());

    System.out.println(newDir + " created successfully : " + newDir.mkdirs());
  }
}
   

It gives the following output,
C:\NEW created successfully : true
C:\NEW2\NEW2_SUB\NEW2_SUB_SUB created successfully : false
C:\NEW2\NEW2_SUB\NEW2_SUB_SUB created successfully : true



 
  


  
bl  br