banner



How To Create A New Directory In Java

Java Program to Create Directories Recursively

Directory/Folder is a File System used in computing that acts as a named memory location for storing related files or even subfolders. This allows better management of files and folders and is premised on the concept of real-world folders used for storing files. This system is the implementation of compartmentalization of the memory and makes the working space more organized. The Directory File System allows hierarchical arrangement as well as nesting of directories inside other directories.

Recursion is the process through which a function calls itself. It is a very useful approach to break down complex problems into smaller subparts.

Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course .

Approaches:

  1. Using the mkdir() method
  2. Using the createDirectory() method of the java.nio package

Approach #1 :


The first approach is to import the java.io.File class and define a method named file() which internally makes use of the mkdir() function to recursively create directories. The algorithm used inside the file() method is described below.

Algorithm :

  1. Create the file() method with return type as void.
  2. This method takes three parameters :
    • String md which stands for Main Directory.
    • String path which stands for the directory structure to be made where each character means a new directory
    • Int depth which represents the number of directories to be made.
  3. Declare the terminating condition as if (depth == 0) return.
  4. Decrement the depth for each recursive call.
  5. Check if the path string is 0 in length and display the message accordingly.
  6. Append the md with the first character of the path string and remove the first character from the path string for each recursive call.
  7. Create an object of the File class with md as a parameter.
  8. Check if the directory already exists using the exists() method and display the message.
  9. Else create the directory using the mkdir()method.
  10. Make the recursive call

Below is the implementation of the above program.

Java

import java.io.File;

class GFG {

static void file(String md, String path, int depth)

{

if (depth == 0 )

return ;

depth -= 1 ;

if (path.length() == 0 )

System.out.println( "Path does not exist" );

else {

md = md + "/" + path.charAt( 0 );

path = path.substring( 1 );

File f = new File(md);

if (f.exists()) {

System.out.println( "The Directory "

+ "already exists" );

}

else {

boolean val = f.mkdir();

if (val)

System.out.println(md + " created"

+ " successfully" );

else

System.out.println(

"Unable to "

+ "create Directory" );

}

}

file(md, path, depth);

}

public static void main(String[] args)

{

GFG ob = new GFG();

ob.file( "/home/mayur/Desktop" , "abcd" , 4 );

}

}

Output:

Approach #2:

This approach makes use of the java.nio package to implement the code. We deploy the createDirectories() method here to create new directories. We also make use of the try-catch block to catch IO Errors. The Algorithm can be found below.

Algorithm:

  1. Repeat steps through 1-6 as mentioned in the algorithm in approach 1.
  2. Now, convert the string md to Path Instance using Path.gets() method.
  3. Again, check if the Directory already exists using the exists() method.
  4. If the Directory is not present, open a try-catch block, and create a new directory using createDirectories() method.
  5. Else, display that the Directory already exists.
  6. Make the recursive call

Below is the implementation of the above program.

Java

import java.nio.file.Paths;

import java.nio.file.Path;

import java.nio.file.Files;

import java.io.IOException;

class GFG {

static void file(String md, String path, int depth)

{

if (depth == 0 )

return ;

depth -= 1 ;

if (path.length() == 0 )

System.out.println( "Path does not exist" );

else {

md = md + "/" + path.charAt( 0 );

path = path.substring( 1 );

Path p = Paths.get(md);

if (!Files.exists(p)) {

try {

Files.createDirectories(p);

System.out.println(md + " created"

+ " successfully" );

}

catch (IOException err) {

err.printStackTrace();

}

}

else

System.out.println( "The directory "

+ "already exists" );

}

file(md, path, depth);

}

public static void main(String[] args)

{

GFG ob = new GFG();

ob.file( "/home/mayur/Desktop" , "klm" , 5 );

}

}

Output:


How To Create A New Directory In Java

Source: https://www.geeksforgeeks.org/java-program-to-create-directories-recursively/

Posted by: galelecought.blogspot.com

0 Response to "How To Create A New Directory In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel