चूंकि Java 7 में Files क्लास पेश की गई थी जिसमें फाइलों, डायरेक्टरीज, या अन्य प्रकार की फाइलों पर काम करने वाले (स्टैटिक) मेथड्स हैं।
createDirectories() मेथड दिए गए डायरेक्टरी को गैर-मौजूदा पैरेंट डायरेक्टरी सहित बनाता है।
उदाहरण
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Demo { public static void main(String args[]) throws IOException { System.out.println("Enter the path to create a directory: "); Scanner sc = new Scanner(System.in); String pathStr = sc.next(); System.out.println("Enter the required directory hierarchy: "); pathStr = pathStr+sc.next(); //पाथ ऑब्जेक्ट बनाना Path path = Paths.get(pathStr); //डायरेक्टरी बनाना Files.createDirectories(path); System.out.println("Directory hierarchy created successfully"); } }आउटपुट
Enter the path to create a directory: D: Enter the required directory hierarchy: sample1/sample2/sapmle3/final_directory Directory hierarchy created successfully
यदि आप सत्यापित करते हैं, तो आप बनाई गई डायरेक्टरी हायरार्की को देख सकते हैं -

आप File के mkdirs() मेथड का उपयोग करके नई डायरेक्टरीज की हायरार्की भी बना सकते हैं। यह मेथड वर्तमान ऑब्जेक्ट द्वारा दर्शाए गए पथ के साथ डायरेक्टरी बनाता है, जिसमें गैर-मौजूदा पैरेंट डायरेक्टरीज शामिल हैं।
उदाहरण
import java.io.File; import java.io.IOException; import java.util.Scanner; public class Demo { public static void main(String args[]) throws IOException { System.out.println("Enter the path to create a directory: "); Scanner sc = new Scanner(System.in); String pathStr = sc.next(); System.out.println("Enter the required directory hierarchy: "); pathStr = pathStr+sc.next(); //File ऑब्जेक्ट बनाना File file = new File(pathStr); //डायरेक्टरी बनाना boolean bool = file.mkdirs(); if(bool){ System.out.println("Directory created successfully"); }else{ System.out.println("Sorry couldn't create specified directory"); } } }आउटपुट
Enter the path to create a directory: D Enter the required directory hierarchy: sample1/sample2/sample3/final_directory
यदि आप सत्यापित करते हैं, तो आप बनाई गई डायरेक्टरी हायरार्की को देख सकते हैं -
