Showing posts with label Java Character Stream. Show all posts
Showing posts with label Java Character Stream. Show all posts

Monday, August 4, 2025

Reading and Writing Characters in Java जावा में करैक्टर पढ़ना और लिखना

Reading and writing characters in Java is done using Character Streams, which handle 16-bit Unicode characters.

जावा में करैक्टर को पढ़ने और लिखने के लिए कैरेक्टर स्ट्रीम्स का उपयोग किया जाता है, जो 16-बिट यूनिकोड करैक्टर को संभालते हैं।

Key Points / मुख्य बिंदु:-

  1. Use FileReader for reading characters from a file.
    फाइल से करैक्टर पढ़ने के लिए FileReader का उपयोग करें।

  2. Use FileWriter for writing characters to a file.
    फाइल में करैक्टर लिखने के लिए FileWriter का उपयोग करें।

  3. Wrap them in BufferedReader and BufferedWriter for efficiency.
    दक्षता बढ़ाने के लिए BufferedReader और BufferedWriter का उपयोग करें।


💻 Example 1: Writing Characters to a File

import java.io.FileWriter;
import java.io.IOException;

public class WriteCharactersExample {
    public static void main(String[] args) {
        try {
            FileWriter writer = new FileWriter("charfile.txt");
            writer.write("Hello, Character Stream!");
            writer.close();
            System.out.println("Characters written successfully.");
        } catch (IOException e) {
            System.out.println("Error writing file.");
        }
    }
}

Output:

Characters written successfully.

charfile.txt content:

Hello, Character Stream!

💻 Example 2: Reading Characters from a File

import java.io.FileReader;
import java.io.IOException;

public class ReadCharactersExample {
    public static void main(String[] args) {
        try {
            FileReader reader = new FileReader("charfile.txt");
            int i;
            while ((i = reader.read()) != -1) {
                System.out.print((char) i);
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("Error reading file.");
        }
    }
}

Output:

Hello, Character Stream!

🔹 Real-life Uses / वास्तविक उपयोग

  • Reading configuration files or notes in plain text
    कॉन्फ़िगरेशन फाइल्स या नोट्स को पढ़ने में

  • Writing logs or reports in text format
    टेक्स्ट फॉर्मेट में लॉग या रिपोर्ट लिखने में

  • Simple data storage for small applications
    छोटी एप्लीकेशन के लिए सरल डेटा स्टोरेज

Character Stream in Java जावा में करैक्टर स्ट्रीम

Character Stream in Java is used to read and write character data (16-bit Unicode) instead of raw bytes.

जावा में करैक्टर स्ट्रीम का प्रयोग करैक्टर डेटा (16-बिट यूनिकोड) को पढ़ने और लिखने के लिए किया जाता है, न कि केवल बाइट्स के लिए।

Character Streams are mainly used for text files because they handle Unicode automatically.
करैक्टर स्ट्रीम्स का प्रयोग प्रायः टेक्स्ट फाइल्स के लिए किया जाता है क्योंकि ये यूनिकोड को स्वतः हैंडल करती हैं।


1️⃣ Important Character Stream Classes

Class Name Description (English) विवरण (Hindi)
FileReader Reads data from file फाइल से डेटा पढ़ता है
FileWriter Writes data to file फाइल में डेटा लिखता है
BufferedReader Reads data using buffer बफर के साथ करैक्टर पढ़ता है
BufferedWriter Writes data using buffer बफर के साथ करैक्टर लिखता है
PrintWriter Writes formatted text output प्रिंट और प्रिंटलन मेथड्स के साथ लिखता है
InputStreamReader Converts bytes to characters बाइट को करैक्टर में बदलता है
OutputStreamWriter Converts characters to bytes करैक्टर को बाइट में बदलता है
Reader Abstract class for character input करैक्टर इनपुट स्ट्रीम्स का सुपरक्लास
Writer Abstract class for character output करैक्टर आउटपुट स्ट्रीम्स का सुपरक्लास

2️⃣ Character Stream Examples

📝 Example 1: Read File using BufferedReader

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileReader fr = new FileReader("file.txt");
            BufferedReader br = new BufferedReader(fr);
            int i;
            while((i = br.read()) != -1) {
                System.out.print((char)i);
            }
            br.close();
            fr.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

📝 Example 2: Write File using BufferedWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write("Hello World!");
            bw.close();
            fw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Data written successfully.
file.txt → Hello World!

📝 Example 3: Write File using PrintWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            PrintWriter pw = new PrintWriter(fw);
            pw.write("Hello Friends!");
            pw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Data written successfully.
file.txt → Hello Friends!

📝 Example 4: Simple File Write using FileWriter

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileWriter fw = new FileWriter("file.txt");
            fw.write("Hello World!");
            fw.close();
            System.out.println("Data written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

📝 Example 5: Simple File Read using FileReader

import java.io.*;

class Sample {
    public static void main(String args[]) {
        try {
            FileReader fr = new FileReader("file.txt");
            int i;
            while((i = fr.read()) != -1) {
                System.out.print((char)i);
            }
            fr.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

📚 Other Stream Classes in Java जावा में अन्य स्ट्रीम क्लासेस

In Java, apart from basic byte and character streams, there are several specialized stream classes that provide advanced input-output operat...