๐ 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 / เคฎुเค्เคฏ เคฌिंเคฆु:-
-
These classes extend InputStream, OutputStream, Reader, Writer.
เคฏे เค्เคฒाเคธेเคธ InputStream, OutputStream, Reader, Writer เคो เคเค्เคธเคेंเคก เคเคฐเคคी เคนैं। -
Provide special operations like file merging, compression, object serialization.
เคฏे เคตिเคถेเคท เคเคชเคฐेเคถเคจ เคैเคธे เคซाเคเคฒ เคฎเคฐ्เคिंเค, เคเคฎ्เคช्เคฐेเคถเคจ, เคเคฌ्เคेเค्เค เคธीเคฐिเคฏเคฒाเคเคेเคถเคจ เคเคฐเคคी เคนैं। -
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
Post a Comment