Java Program to Create Directories Recursively
A 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.
- Using the mkdir() method
- Using the createDirectory() method of the java.nio package
1. Using the mkdir() Method
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.
- Create the file() method with the return type as void.
- 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 represents the number of directories to be made.
- Declare the terminating condition as if (depth == 0) return.
- Decrement the depth for each recursive call.
- Check if the path string is 0 in length and display the message accordingly.
- Append the md with the first character of the path string and remove the first character from the path string for each recursive call.
- Create an object of the File class with md as a parameter.
- Check if the directory already exists using the exists() method and display the message.
- Else create the directory using the mkdir()method.
- Make the recursive call.
Implementation: Below is the implementation of the above program.
Time complexity: O(n),The time complexity of the above program is O(n) as the program is iterating through the string characters one by one to create the directories.
Space complexity: O(1),The space complexity of the above program is O(1) as no extra space is required for execution.
2. Using the createDirectory() method of the java.nio package
This approach makes use of 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.
- Repeat steps through 1-6 as mentioned in the algorithm in approach 1.
- Now, convert the string md to Path Instance using Path.gets() method.
- Again, check if the Directory already exists using the exists() method.
- If the Directory is not present, open a try-catch block, and create a new directory using createDirectories() method.
- Else, display that the Directory already exists.
- Make the recursive call
Below is the implementation of the above program.