๐Ÿ“š 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 operations.

เคœाเคตा เคฎें, เคฌेเคธिเค• เคฌाเค‡เคŸ เค”เคฐ เค•ैเคฐेเค•्เคŸเคฐ เคธ्เคŸ्เคฐीเคฎ्เคธ เค•े เค…เคฒाเคตा, เค•เคˆ เคตिเคถेเคท เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ เคนोเคคी เคนैं เคœो เคเคกเคตांเคธ เค‡เคจเคชुเคŸ-เค†เค‰เคŸเคชुเคŸ เค‘เคชเคฐेเคถเคจ เคช्เคฐเคฆाเคจ เค•เคฐเคคी เคนैं।


Key Points / เคฎुเค–्เคฏ เคฌिंเคฆु:-

  1. These classes extend InputStream, OutputStream, Reader, Writer.
    เคฏे เค•्เคฒाเคธेเคธ InputStream, OutputStream, Reader, Writer เค•ो เคเค•्เคธเคŸेंเคก เค•เคฐเคคी เคนैं।

  2. Provide special operations like file merging, compression, object serialization.
    เคฏे เคตिเคถेเคท เค‘เคชเคฐेเคถเคจ เคœैเคธे เคซाเค‡เคฒ เคฎเคฐ्เคœिंเค—, เค•เคฎ्เคช्เคฐेเคถเคจ, เค‘เคฌ्เคœेเค•्เคŸ เคธीเคฐिเคฏเคฒाเค‡เคœेเคถเคจ เค•เคฐเคคी เคนैं।

  3. Often used in real-time applications where basic streams are not enough.
    เค‡เคจเค•ा เคช्เคฐเคฏोเค— เคฐीเคฏเคฒ-เคŸाเค‡เคฎ เคเคช्เคฒिเค•ेเคถเคจ เคฎें เคนोเคคा เคนै เคœเคนाँ เคฌेเคธिเค• เคธ्เคŸ्เคฐीเคฎ เคชเคฐ्เคฏाเคช्เคค เคจเคนीं เคนोเคคी।


๐Ÿ”น Common Other Stream Classes / เคธाเคฎाเคจ्เคฏ เค…เคจ्เคฏ เคธ्เคŸ्เคฐीเคฎ เค•्เคฒाเคธेเคธ

Stream Class Use / เค‰เคชเคฏोเค—
SequenceInputStream Combine multiple input streams
PushbackInputStream         Push data back into stream
FilterInputStream Modify input data before use
FilterOutputStream Modify output data before writing
PrintStream Print formatted data to output
ObjectInputStream Read objects from a stream
ObjectOutputStream Write objects to a stream
PipedInputStream Read data from another thread (pipe)
PipedOutputStream Send data to another thread (pipe)

๐Ÿ’ป Example 1: SequenceInputStream

Combining two files into one output file.
เคฆो เคซाเค‡เคฒ्เคธ เค•ो เคœोเคก़เค•เคฐ เคเค• เค†เค‰เคŸเคชुเคŸ เคซाเค‡เคฒ เคฎें เคฒिเค–เคจा।

import java.io.*;

public class SequenceStreamExample {
    public static void main(String[] args) throws IOException {
        FileInputStream fis1 = new FileInputStream("file1.txt");
        FileInputStream fis2 = new FileInputStream("file2.txt");
        SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

        FileOutputStream fos = new FileOutputStream("combined.txt");
        int i;
        while ((i = sis.read()) != -1) {
            fos.write(i);
        }
        sis.close();
        fos.close();
        System.out.println("Files combined successfully.");
    }
}

Sample Output:

Files combined successfully.
combined.txt → [Contents of file1 + file2]

๐Ÿ’ป Example 2: PrintStream for Formatted Output

import java.io.*;

public class PrintStreamExample {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("output.txt");
        PrintStream ps = new PrintStream(fos);

        ps.println("Hello, World!");
        ps.printf("Number: %d, Pi: %.2f", 10, 3.1415);

        ps.close();
        fos.close();
        System.out.println("Data written using PrintStream.");
    }
}

Sample Output (output.txt):

Hello, World!
Number: 10, Pi: 3.14

๐Ÿ’ป Example 3: ObjectOutputStream and ObjectInputStream

Serializing and deserializing an object.
เค‘เคฌ्เคœेเค•्เคŸ เค•ो เคธेเคต เค•เคฐเคจा เค”เคฐ เคตाเคชเคธ เคชเคข़เคจा।

import java.io.*;

class Student implements Serializable {
    int id;
    String name;
    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class ObjectStreamExample {
    public static void main(String[] args) throws Exception {
        // Writing object
        FileOutputStream fos = new FileOutputStream("student.dat");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(new Student(101, "Aman"));
        oos.close();
        fos.close();

        // Reading object
        FileInputStream fis = new FileInputStream("student.dat");
        ObjectInputStream ois = new ObjectInputStream(fis);
        Student s = (Student) ois.readObject();
        ois.close();
        fis.close();

        System.out.println("Student ID: " + s.id + ", Name: " + s.name);
    }
}

Sample Output:

Student ID: 101, Name: Aman

๐Ÿ”น Real-life Uses / เคตाเคธ्เคคเคตिเค• เค‰เคชเคฏोเค—

  • File Merging / Compression
    เคซाเค‡เคฒ เคฎเคฐ्เคœिंเค— / เค•เคฎ्เคช्เคฐेเคถเคจ

  • Storing and Reading Objects (Serialization)
    เค‘เคฌ्เคœेเค•्เคŸ्เคธ เค•ो เคธेเคต เค”เคฐ เคฐीเคก เค•เคฐเคจा (เคธीเคฐिเคฏเคฒाเค‡เคœेเคถเคจ)

  • Inter-thread Communication using Piped Streams
    เคฅ्เคฐेเคก्เคธ เค•े เคฌीเคš เคธंเคšाเคฐ เคชाเค‡เคช्เคก เคธ्เคŸ्เคฐीเคฎ เคฆ्เคตाเคฐा

Comments

Popular posts from this blog

What is a Web Browser? เคตेเคฌ เคฌ्เคฐाเค‰เคœ़เคฐ เค•्เคฏा เคนै?

Java's Support System เคœाเคตा เค•ा เคธเคนเคฏोเค—ी เคคंเคค्เคฐ

The Internet and Java เค‡ंเคŸเคฐเคจेเคŸ เค”เคฐ เคœाเคตा เค•ा เคธเคฎ्เคฌเคจ्เคง