Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to create new directory and sub-directories in Java ?.

Program to demonstrate how to create new directory and sub-directories in Java.

package com.hubberspot.io;

import java.io.File;

public class CreateNewFolder {

 public static void main(String[] args) {

  // Create a File Object and pass directory/folder name
  // as a string to it.  
  File fileStructure = new File("c:\\New Folder");

  // File object has a method called as exists() which
  // Tests whether the file or directory denoted by 
  // this abstract pathname exists. 
  if(! fileStructure.exists()) {

   // File object has a method called as mkdir() which
   // Creates the directory named by this abstract pathname. 
   if (fileStructure.mkdir()) {

    System.out.println("New Folder/Directory created .... ");

   }
   else {

    System.out.println("Oops!!! Something blown up file creation...");

   }

  } else {

   System.out.println("File already exists !!! ...");

  }

  // Create a File object and pass as a string full file structure
  // you want to create. 
  File subFiles = new File("c:\\Directory\\Sub-Folder\\Sub-Folder2");

  // File object has a method by name mkdirs() which
  // Creates the directory named by this abstract pathname,
  // it creates full file structure as it is given in as string
  // Note that if this operation fails it may have succeeded 
  // in creating some of the necessary parent directories. 

  if(subFiles.mkdirs()) {

   System.out.println("Full directory structure created ... ");

  }
  else {

   System.out.println("Oops!!! Something blown up files creation...");

  }

 }

}



Output of the program :



 
© 2021 Learn Java by Examples Template by Hubberspot