কম্পিউটার

কিভাবে জাভা ব্যবহার করে একটি .txt ফাইলে একটি লাইন ওভাররাইট করবেন?


API ব্যবহার করা হয়েছে

প্রতিস্থাপন করুন() স্ট্রিং ক্লাসের পদ্ধতি একটি নিয়মিত অভিব্যক্তি এবং একটি প্রতিস্থাপন স্ট্রিং প্রতিনিধিত্বকারী দুটি স্ট্রিং গ্রহণ করে এবং প্রদত্ত স্ট্রিং দিয়ে মিলিত মানগুলি প্রতিস্থাপন করে৷

java.util ক্লাস (নির্মাণকারী) একটি ফাইল, ইনপুটস্ট্রিম, পাথ এবং স্ট্রিং অবজেক্ট গ্রহণ করে, রেগুলার এক্সপ্রেশন ব্যবহার করে টোকেন দ্বারা সমস্ত আদিম ডেটা টাইপ এবং স্ট্রিংগুলি (প্রদত্ত উত্স থেকে) পড়ে। প্রদত্ত পরবর্তীXXX() পদ্ধতিগুলি ব্যবহার করে উৎস থেকে বিভিন্ন ডেটাটাইপ পড়তে।

স্ট্রিংবাফার ক্লাস হল স্ট্রিং-এর একটি পরিবর্তনযোগ্য বিকল্প, এই ক্লাসটি ইনস্ট্যান্ট করার পরে আপনি append() পদ্ধতি ব্যবহার করে এতে ডেটা যোগ করতে পারেন।

প্রক্রিয়া

একটি ফাইলের একটি নির্দিষ্ট লাইন ওভাররাইট করতে -

স্ট্রিং-

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

  • স্ক্যানার ক্লাসটি প্যারামিটার হিসাবে ফাইলটি তার কনস্ট্রাক্টরের কাছে পাস করে ইনস্ট্যান্টিয়েট করুন।

  • একটি খালি StringBuffer অবজেক্ট তৈরি করুন৷

  • অ্যাপেন্ড() পদ্ধতি ব্যবহার করে স্ট্রিংবাফার অবজেক্টে লাইন দ্বারা ফাইল লাইনের বিষয়বস্তু যোগ করুন।

  • toString() পদ্ধতি ব্যবহার করে স্ট্রিংবাফারকে স্ট্রিং-এ রূপান্তর করুন।

  • স্ক্যানার অবজেক্ট বন্ধ করুন।

প্রতিস্থাপন করুন() প্রাপ্ত স্ট্রিং-এর পদ্ধতি যা প্রতিস্থাপনের লাইন (পুরাতন লাইন) এবং প্রতিস্থাপন লাইন (নতুন লাইন) পরামিতি হিসাবে পাস করে।

ফাইলের বিষয়বস্তু পুনরায় লিখুন −

  • ফাইল রাইটার ক্লাস ইনস্ট্যান্ট করুন।

  • Append() পদ্ধতি ব্যবহার করে FileWriter অবজেক্টের replaceAll() পদ্ধতির ফলাফল যোগ করুন।

  • ফ্লাশ() পদ্ধতি ব্যবহার করে ফাইলে যোগ করা ডেটা পুশ করুন।

উদাহরণ

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OverwriteLine {
   public static void main(String args[]) throws IOException {
      //Instantiating the File class
      String filePath = "D://input.txt";
      //Instantiating the Scanner class to read the file
      Scanner sc = new Scanner(new File(filePath));
      //instantiating the StringBuffer class
      StringBuffer buffer = new StringBuffer();
      //Reading lines of the file and appending them to StringBuffer
      while (sc.hasNextLine()) {
         buffer.append(sc.nextLine()+System.lineSeparator());
      }
      String fileContents = buffer.toString();
      System.out.println("Contents of the file: "+fileContents);
      //closing the Scanner object
      sc.close();
      String oldLine = "No preconditions and no impediments. Simply Easy Learning!";
      String newLine = "Enjoy the free content";
      //Replacing the old line with new line
      fileContents = fileContents.replaceAll(oldLine, newLine);
      //instantiating the FileWriter class
      FileWriter writer = new FileWriter(filePath);
      System.out.println("");
      System.out.println("new data: "+fileContents);
      writer.append(fileContents);
      writer.flush();
   }
}

আউটপুট

Contents of the file: Tutorials Point originated from the idea that there exists a 
class of readers who respond better to online content and prefer to learn new skills.
Our content and resources are freely available and we prefer to keep it that way to 
encourage our readers acquire as many skills as they would like to.
We don’t force our readers to sign up with us or submit their details either.
No preconditions and no impediments. Simply Easy Learning!

new data: Tutorials Point originated from the idea that there exists a class of readers 
who respond better to online content and prefer to learn new skills.
Our content and resources are freely available and we prefer to keep it that way to 
encourage our readers acquire as many skills as they would like to.
We don’t force our readers to sign up with us or submit their details either.
Enjoy the free content

  1. জাভা ব্যবহার করে ওপেনসিভিতে কীভাবে একটি তীরযুক্ত রেখা আঁকবেন?

  2. কিভাবে জাভা ব্যবহার করে OpenCV এ একটি লাইন আঁকবেন?

  3. কিভাবে জাভা ব্যবহার করে একটি ডিরেক্টরি অনুক্রম তৈরি করবেন?

  4. কিভাবে জাভা ব্যবহার করে একটি JSON ফাইল লিখবেন/তৈরি করবেন?