चूंकि Java 7 में Files क्लास पेश की गई थी जिसमें फाइलों, डायरेक्टरीज, या अन्य प्रकार की फाइलों पर काम करने वाले (स्टैटिक) मेथड्स हैं।
createDirectory() मेथड Files क्लास का आवश्यक डायरेक्टरी का पथ स्वीकार करता है और एक नई डायरेक्टरी बनाता है।
उदाहरण
निम्नलिखित जावा उदाहरण उपयोगकर्ता से बनाई जाने वाली डायरेक्टरी का पथ और नाम पढ़ता है, और इसे बनाता है।
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 Test { 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 name of the desired a directory: "); pathStr = pathStr+sc.next(); //पाथ ऑब्जेक्ट बनाना Path path = Paths.get(pathStr); //डायरेक्टरी बनाना Files.createDirectory(path); System.out.println("Directory created successfully"); } }आउटपुट
Enter the path to create a directory: D: Enter the name of the desired a directory: sample_directory Directory created successfully
यदि आप सत्यापित करते हैं, तो आप बनाई गई डायरेक्टरी को देख सकते हैं -

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 Test { 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 name of the desired a directory: "); pathStr = pathStr+sc.next(); //पाथ ऑब्जेक्ट बनाना Path path = Paths.get(pathStr); //डायरेक्टरी बनाना Files.createDirectories(path); System.out.println("Directories created successfully"); } }आउटपुट
Enter the path to create a directory: D: Enter the name of the desired a directory: sample/test1/test2/test3/final_folder Directory created successfully
यदि आप सत्यापित करते हैं, तो आप बनाई गई डायरेक्टरी को देख सकते हैं -