Showing posts with label reading writing characters in Java. Show all posts
Showing posts with label reading writing characters in Java. 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
    छोटी एप्लीकेशन के लिए सरल डेटा स्टोरेज

📚 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...