Showing posts with label FileWriter. Show all posts
Showing posts with label FileWriter. Show all posts

Monday, August 4, 2025

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!

जावा में फ़ाइल हैंडलिंग (Java File Handling in Hindi & English)

File handling in Java allows us to read and write data permanently in files. Using Java I/O (Input/Output) streams, we can store data, read it, and even process it efficiently.

जावा में फ़ाइल हैंडलिंग हमें डेटा को स्थायी रूप से सेव और पढ़ने की सुविधा देती है। जावा I/O (इनपुट/आउटपुट) स्ट्रीम्स की मदद से हम डेटा को स्टोर, पढ़ और प्रोसेस कर सकते हैं।


1️⃣ जावा में स्ट्रीम (Java Streams)

stream is a sequence of data. In Java, streams are used to read and write data from one location to another (like file, memory, or network).
स्ट्रीम डेटा का एक क्रम होता है। जावा में, स्ट्रीम का उपयोग डेटा पढ़ने और लिखने के लिए किया जाता है, जैसे फ़ाइल, मेमोरी या नेटवर्क।

Java I/O streams are mainly divided into two types:
जावा I/O स्ट्रीम मुख्यतः दो प्रकार की होती हैं:

1️⃣ Character Stream – कैरेक्टर (2 बाइट यूनिकोड) डेटा के लिए

  • Character Stream → Reader और Writer क्लासेस


2️⃣ Byte Stream – बाइट (8-बिट) डेटा के लिए

  • Byte Stream → InputStream और OutputStream क्लासेस

  • Buffering के लिए → BufferedReaderBufferedWriterBufferedInputStreamBufferedOutputStream

  • Printing और Formatting के लिए → PrintWriterPrintStream

  • फ़ाइल में डेटा को स्थायी रूप से सेव कर सकते हैं। बड़ी फाइल्स को पढ़ना और लिखना आसान। स्ट्रीम्स और बफरिंग से परफॉर्मेंस बेहतर। नेटवर्किंग और डेटाबेस ऑपरेशन में डाटा स्ट्रीमिंग आसान।


2️⃣ Character Stream (कैरेक्टर स्ट्रीम)

Character streams are used to read and write character data.
कैरेक्टर स्ट्रीम का प्रयोग कैरेक्टर डेटा को पढ़ने और लिखने के लिए किया जाता है।

✨ Common Character Stream Classes

Class NameDescription (English)विवरण (Hindi)
FileReaderReads data from a file (character by character)फाइल से कैरेक्टर डेटा पढ़ता है
FileWriterWrites data to a fileफाइल में कैरेक्टर डेटा लिखता है
BufferedReaderReads text from an input stream efficientlyबफर के साथ कैरेक्टर पढ़ता है
BufferedWriterWrites text to an output stream efficientlyबफर के साथ कैरेक्टर लिखता है
PrintWriterAllows printing formatted textफॉर्मेटेड टेक्स्ट प्रिंट करने की सुविधा

📝 Example 1: Reading File using BufferedReader

import java.io.*;

class ReadFileExample {
    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("file.txt");
            BufferedReader br = new BufferedReader(fr);
            String line;
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
            br.close();
            fr.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

Hello World!

📝 Example 2: Writing File using FileWriter

import java.io.*;

class WriteFileExample {
    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);
        }
    }
}

3️⃣ Byte Stream (बाइट स्ट्रीम)

Byte streams are used to handle binary data like images, videos, or audio files.
बाइट स्ट्रीम का प्रयोग बाइनरी डेटा जैसे इमेज, वीडियो या ऑडियो को पढ़ने-लिखने में किया जाता है।

✨ Common Byte Stream Classes

Class NameDescription (English)विवरण (Hindi)
FileInputStreamReads bytes from a fileफाइल से बाइट पढ़ता है
FileOutputStreamWrites bytes to a fileफाइल में बाइट लिखता है
BufferedInputStreamReads data with bufferingबफर के साथ बाइट पढ़ता है
BufferedOutputStreamWrites data with bufferingबफर के साथ बाइट लिखता है
DataInputStreamReads primitive data typesप्रिमिटिव डेटा पढ़ता है
DataOutputStreamWrites primitive data typesप्रिमिटिव डेटा लिखता है
PrintStreamPrints formatted outputफॉर्मेटेड आउटपुट लिखता है

📝 Example 3: Reading File using FileInputStream

import java.io.*;

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

📝 Example 4: Writing File using FileOutputStream

import java.io.*;

class FileOutputExample {
    public static void main(String[] args) {
        try {
            FileOutputStream fos = new FileOutputStream("file.txt");
            String str = "Hello Java!";
            fos.write(str.getBytes());
            fos.close();
            System.out.println("File written successfully.");
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

Output:

File written successfully.

4️⃣ फ़ाइल हैंडलिंग के फायदे (Advantages)

  • Permanent data storage (स्थायी स्टोरेज)

  • Easy reading/writing of large files (बड़ी फ़ाइल पढ़ना/लिखना आसान)

  • Supports both text and binary data

  • Useful in database, networking and real-time 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...