কম্পিউটার

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


তালিকা ফাইল() ফাইলের পদ্ধতি ক্লাস বর্তমান (ফাইল) অবজেক্ট দ্বারা উপস্থাপিত পাথে সমস্ত ফাইলের (এবং ডিরেক্টরিগুলির) অবজেক্ট (বিমূর্ত পথ) ধারণ করে একটি অ্যারে প্রদান করে।

একটি ফোল্ডারের সমস্ত ফাইলের বিষয়বস্তু একটি একক ফাইলে পড়তে -

  • প্যারামিটার হিসাবে প্রয়োজনীয় ফাইল পাথ পাস করে একটি ফাইল অবজেক্ট তৈরি করুন।
  • স্ক্যানার বা অন্য কোন রিডার ব্যবহার করে প্রতিটি ফাইলের বিষয়বস্তু পড়ুন।
  • পঠিত বিষয়বস্তু একটি স্ট্রিংবাফারে যুক্ত করুন।
  • প্রয়োজনীয় আউটপুট ফাইলে 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 {
      //Creating a File object for directory
      File directoryPath = new File("D:\\SampleDirectory");
      //List of all files and directories
      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());
         //Instantiating the Scanner class
         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(" ");        
           //Instantiating the FileOutputStream class
         FileOutputStream fileOut = new FileOutputStream("D:\\output.txt");
         //Instantiating the DataOutputStream class
         DataOutputStream outputStream = new DataOutputStream(fileOut);
         //Writing UTF data to the output stream
         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. কিভাবে একটি পান্ডাস ডেটাফ্রেম হিসাবে একটি ডিরেক্টরির অধীনে সমস্ত এক্সেল ফাইল পড়তে হয়?