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

জাভাতে ফোল্ডারের সব ফাইল পড়ে একটি ফাইলে লেখা

File ক্লাসের listFiles() মেথড করেন্ট (File) অবজেক্টের পাথে থাকা সব ফাইল (এবং ডাইরেক্টরি) এর অবজেক্ট (অবস্ট্র্যাক্ট পাথ) ধারণকারী একটি অ্যারে ফেরত দেয়।

এক ফোল্ডারের সব ফাইলের কন্টেন্ট একটি ফাইলে পড়ে লেখার জন্য:

  • প্রয়োজনীয় ফাইল পাথ পাস করে একটি File অবজেক্ট তৈরি করুন।
  • Scanner বা অন্য কোনো রিডার (reader) ব্যবহার করে প্রতিটি ফাইলের কন্টেন্ট পড়ুন।
  • পড়া কন্টেন্ট StringBuffer-এ অ্যাপেন্ড (append) করুন।
  • StringBuffer-এর কন্টেন্ট প্রয়োজনীয় আউটপুট ফাইলে লিখুন।

উদাহরণ

import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Scanner; 

public class Test { 
    public static void main(String args[]) throws IOException { 
        // ডাইরেক্টরি জন্য File অবজেক্ট তৈরি
        File directoryPath = new File("D:\\SampleDirectory"); 
        
        // সব ফাইল ও ডাইরেক্টরির লিস্ট
        File filesList[] = directoryPath.listFiles(); 
        System.out.println("List of files and directories in the specified directory:"); 
        
        Scanner sc = null; 
        StringBuffer sb = new StringBuffer(); 
        
        for (File file : filesList) { 
            System.out.println("File name: " + file.getName()); 
            System.out.println("File path: " + file.getAbsolutePath()); 
            System.out.println("Size: " + file.getTotalSpace()); 
            
            // Scanner ক্লাস ইনস্ট্যানশিয়েট করা
            sc = new Scanner(file); 
            String input; 
            
            while (sc.hasNextLine()) { 
                input = sc.nextLine(); 
                sb.append(input + " "); 
            } 
            
            System.out.println("Contents of the file: " + sb.toString()); 
            System.out.println(" "); 
            
            // FileOutputStream ক্লাস ইনস্ট্যানশিয়েট করা
            FileOutputStream fileOut = new FileOutputStream("D:\\output.txt"); 
            
            // DataOutputStream ক্লাস ইনস্ট্যানশিয়েট করা
            DataOutputStream outputStream = new DataOutputStream(fileOut); 
            
            // আউটপুট স্ট্রিমে UTF ডেটা লেখা
            outputStream.write(sb.toString().getBytes()); 
            outputStream.flush(); 
            
            System.out.println("Data entered into the file"); 
        } 
    } 
}

আউটপুট

List of files and directories in the specified directory: 
File name: sample1.txt 
File path: D:\SampleDirectory\sample1.txt 
Contents of the file: sample text file1 
Data entered into the file 
File name: sample2.txt 
File path: D:\SampleDirectory\sample2.txt 
Contents of the file: sample text file2 
Data entered into the file 
File name: sample3.txt 
File path: D:\SampleDirectory\sample3.txt 
Contents of the file: sample text file3 
Data entered into the file
  1. জাভাতে CSV ফাইল থেকে ডেটা পড়া

  2. জাভাতে প্রপার্টিজ ফাইল থেকে ডেটা পড়া

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

  4. পাইথনে ডিরেক্টরির সব এক্সেল ফাইল পান্ডাস ডেটাফ্রেম হিসেবে পড়া (glob + read_excel)