Showing posts with label SequenceInputStream example. Show all posts
Showing posts with label SequenceInputStream example. Show all posts

Monday, August 4, 2025

๐Ÿ“š 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
    เคฅ्เคฐेเคก्เคธ เค•े เคฌीเคš เคธंเคšाเคฐ เคชाเค‡เคช्เคก เคธ्เคŸ्เคฐीเคฎ เคฆ्เคตाเคฐा

Concatenating and Buffering Files in Java เคœाเคตा เคฎें เคซ़ाเค‡เคฒों เค•ो เคœोเคก़เคจा เค”เคฐ เคฌเคซเคฐ เค•เคฐเคจा

In Java, we can merge multiple files into a single file (concatenation) and use buffering to improve file I/O performance.

เคœाเคตा เคฎें เคนเคฎ เค•เคˆ เคซाเค‡เคฒों เค•ो เคเค• เคนी เคซाเค‡เคฒ เคฎें เคœोเคก़ เคธเค•เคคे เคนैं (เค•ंเค•ैเคŸिเคจेเคถเคจ) เค”เคฐ เคฌเคซเคฐिंเค— เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคซाเค‡เคฒ I/O เค•ी เค—เคคि เคฌเคข़ा เคธเค•เคคे เคนैं।


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

  1. Concatenation: Combining multiple files into one.
    เค•ंเค•ैเคŸिเคจेเคถเคจ: เค•เคˆ เคซाเค‡เคฒों เค•ो เคเค• เคซाเค‡เคฒ เคฎें เคœोเคก़เคจा।

  2. Buffering: Using a buffer to reduce disk access and improve speed.
    เคฌเคซเคฐिंเค—: เคฌเคซเคฐ เค•ा เค‰เคชเคฏोเค— เค•เคฐเค•े เคกिเคธ्เค• เคเค•्เคธेเคธ เค•เคฎ เค•เคฐเคจा เค”เคฐ เค—เคคि เคฌเคข़ाเคจा।

  3. Streams Used: SequenceInputStream, BufferedInputStream, BufferedOutputStream.
    เค‰เคชเคฏोเค— เค•ी เค—เคˆ เคธ्เคŸ्เคฐीเคฎ्เคธ: SequenceInputStream, BufferedInputStream, BufferedOutputStream


๐Ÿ’ป Example 1: Concatenating Two Files

import java.io.*;

public class ConcatFiles {
    public static void main(String[] args) {
        try {
            FileInputStream fis1 = new FileInputStream("file1.txt");
            FileInputStream fis2 = new FileInputStream("file2.txt");

            SequenceInputStream sis = new SequenceInputStream(fis1, fis2);
            FileOutputStream fos = new FileOutputStream("merged.txt");

            int data;
            while ((data = sis.read()) != -1) {
                fos.write(data);
            }

            sis.close();
            fis1.close();
            fis2.close();
            fos.close();

            System.out.println("Files concatenated successfully!");
        } catch (IOException e) {
            System.out.println("Error: " + e);
        }
    }
}

Input Files:
file1.txt → Hello
file2.txt → World

Output File (merged.txt):

Hello World

๐Ÿ’ป Example 2: Buffering for Faster File Copy

import java.io.*;

public class BufferFileCopy {
    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("source.txt");
            BufferedInputStream bis = new BufferedInputStream(fis);

            FileOutputStream fos = new FileOutputStream("copy.txt");
            BufferedOutputStream bos = new BufferedOutputStream(fos);

            int data;
            while ((data = bis.read()) != -1) {
                bos.write(data);
            }

            bis.close();
            bos.close();
            System.out.println("File copied with buffering successfully!");
        } catch (IOException e) {
            System.out.println("File operation failed: " + e);
        }
    }
}

Output:

File copied with buffering successfully!

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

  • Merging multiple log files into a single file.
    เค•เคˆ เคฒॉเค— เคซाเค‡เคฒ्เคธ เค•ो เคเค• เคนी เคซाเค‡เคฒ เคฎें เคœोเคก़เคจा।

  • Copying large files efficiently with buffers.
    เคฌเคก़े เคซाเค‡เคฒ्เคธ เค•ो เคคेเคœ़ी เคธे เค•ॉเคชी เค•เคฐเคจा।

  • Data backup utilities use concatenation and buffering.
    เคกेเคŸा เคฌैเค•เค…เคช เคฏूเคŸिเคฒिเคŸी เคฎें เค‡เคธเค•ा เค‰เคชเคฏोเค— เคนोเคคा เคนै।

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