কম্পিউটার

জাভা ফাইলের উদাহরণ লিখুন

এই পোস্টে আমরা জাভা ব্যবহার করে একটি ফাইলে কীভাবে লিখতে হয় তার পাঁচটি ভিন্ন উদাহরণ দেখব। কোড sinppets ফাইলটিতে লেখার আগে ফাইলটি বিদ্যমান কিনা তা পরীক্ষা করে, অন্যথায় একটি ফাইল তৈরি করা হয়।

বাফারডরাইটার ব্যবহার করে ফাইলে লিখুন

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}
দ্রষ্টব্য:যদি আমরা একটি ফাইলে যুক্ত করতে চাই, তাহলে আমাদের FileWriter আরম্ভ করতে হবে সত্যের সাথে প্যারামিটার:
FileWriter fw = new FileWriter(file, true);

সম্পর্কিত:

  • কিভাবে জাভাতে একটি ফাইল তৈরি করবেন
  • কিভাবে জাভাতে বর্তমান কাজের ডিরেক্টরি পেতে হয়

প্রিন্ট রাইটার ব্যবহার করে ফাইলে লিখুন

import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file);

            PrintWriter bw = new PrintWriter(fw);
            bw.write(content);
            bw.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

FileOutputStream ব্যবহার করে ফাইলে লিখুন

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteToFile {
    public static void main( String[] args ) {
        try {
            String content = "Content to write to file";
            //Name and path of the file
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream outStream = new FileOutputStream(file);
            byte[] strToBytes = content.getBytes();
            outStream.write(strToBytes);

            outStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

ফাইল ক্লাস ব্যবহার করে ফাইলে লিখুন

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class WriteToFile {
    public static void main( String[] args ) {
        Path path = Paths.get("writefile.txt");
        String content = "Content to write to file";

        try {
            byte[] bytes = content.getBytes();
            Files.write(path, bytes);
        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

ডেটাআউটপুটস্ট্রিম ব্যবহার করে ফাইলে লিখুন

import java.io.*;

public class WriteToFile {
    public static void main( String[] args ) {
        String content = "Content to write to file";

        try {
            File file = new File("writefile.txt");

            if(!file.exists()){
                file.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            DataOutputStream dataOutStream = new DataOutputStream(bos);
            dataOutStream.writeUTF(content);
            dataOutStream.close();

        } catch(IOException ex) {
            System.out.println("Exception occurred:");
            ex.printStackTrace();
        }
    }
}

  1. জাভাতে Gson লাইব্রেরি ব্যবহার করে ফাইলে একটি JSON স্ট্রিং কীভাবে লিখবেন?

  2. কিভাবে আমরা জাভাতে একটি ফাইলে JSON অবজেক্ট লিখতে পারি?

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

  4. রুবিতে ফাইলগুলি কীভাবে পড়তে এবং লিখতে হয় (উদাহরণ সহ)