কম্পিউটার টিউটোরিয়াল

জাভাতে File ইউটিলিটি মেথড ব্যবহার করে ডাইরেক্টরি তৈরি করা

Java 7 থেকে Files ক্লাস ইন্ট্রোডিউস করা হয়েছে যেটি ফাইল, ডাইরেক্টরি বা অন্যান্য টাইপের ফাইলের ওপর অপারেশন করার স্ট্যাটিক (static) মেথড ধারণ করে।

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 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

ভেরিফাই করলে তৈরি ডাইরেক্টরি দেখতে পাবেন:

জাভাতে File ইউটিলিটি মেথড ব্যবহার করে ডাইরেক্টরি তৈরি করা

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 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

ভেরিফাই করলে তৈরি ডাইরেক্টরি দেখতে পাবেন:

জাভাতে File ইউটিলিটি মেথড ব্যবহার করে ডাইরেক্টরি তৈরি করা

  1. জাভাতে ডিরেক্টরি হায়ারার্কি (Directory Hierarchy) কীভাবে তৈরি করবেন?

  2. জাভাতে Gson লাইব্রেরি ব্যবহার করে JSON স্ট্রিং ফাইলে লেখার পদ্ধতি

  3. জাভাতে JSON ফাইল কীভাবে লিখবেন/তैयার করবেন?

  4. Tkinter filedialog-এ ফাইল পাথ স্পেসিফাই করার পদ্ধতি