Skip to content

Commit 26e4e7c

Browse files
committed
feat: implement reading student data using DataInputStream
WHAT: - Added `PrintInputStreamExample` to demonstrate deserialization of primitive data types using `DataInputStream`. - Reads a `Student1` object’s fields in the same order they were written with `DataOutputStream`. HOW: - Used `FileInputStream` wrapped with `DataInputStream` for reading binary data. - Read fields sequentially: - `rollno` (int) - `name` (UTF string) - `dept` (UTF string) - `avg` (float) - Printed the values to verify correctness. WHY: - Ensures consistent reading of binary data across different runs. - Demonstrates importance of maintaining the same read/write order during serialization. REAL-WORLD APPLICATIONS: - Storing and retrieving structured records (students, employees, transactions) in lightweight systems. - Useful for applications where performance and storage efficiency are prioritized over human readability. - Can serve as a foundation for building simple binary-based data persistence layers. NOTES: - The file `Student2.txt` must have been written using `DataOutputStream` with the same field order. - Misalignment in read/write order can cause corrupted or invalid data reads. - Unlike object serialization (`ObjectOutputStream`), this approach works only with primitive types and strings. NEXT IMPROVEMENTS: - Add error handling for `EOFException` to gracefully detect end-of-file. - Wrap streams in `try-with-resources` for automatic closing. - Extend program to handle arrays or lists of `Student1` records. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8298ff8 commit 26e4e7c

File tree

1 file changed

+45
-14
lines changed

1 file changed

+45
-14
lines changed
Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,55 @@
11
import java.io.*;
22

3-
class PrintInputStreamExample {
3+
public class PrintInputStreamExample {
44
public static void main(String[] args) throws Exception {
5-
FileInputStream fis = new FileInputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Student1.txt");
6-
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
5+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Student2.txt");
6+
DataInputStream dis = new DataInputStream(fis);
77

8-
// Creating a Student object
9-
Student s = new Student();
8+
// Read binary data in the correct order.
9+
Student1 s = new Student1();
10+
s.rollno = dis.readInt();
11+
s.name = dis.readUTF();
12+
s.dept = dis.readUTF();
13+
s.avg = dis.readFloat();
1014

11-
// Reading data from file
12-
s.rollno = Integer.parseInt(br.readLine()); // Reading roll number
13-
s.name = br.readLine(); // Reading name
14-
s.dept = br.readLine(); // Reading department
15-
16-
// Printing Student details
1715
System.out.println("Roll No: " + s.rollno);
1816
System.out.println("Name: " + s.name);
1917
System.out.println("Dept: " + s.dept);
18+
System.out.println("Average: " + s.avg);
2019

21-
// Closing the reader
22-
br.close();
20+
dis.close();
21+
fis.close();
2322
}
24-
}
23+
}
24+
25+
/*
26+
1. DataInputStream:
27+
- Binary data ko read karne ke liye use hota hai (not plain text).
28+
- Must read in the same order jisme write kiya tha (DataOutputStream se).
29+
30+
2. Student1 object fields
31+
- `s.rollno = dis.readInt();`
32+
- `s.name = dis.readUTF();`
33+
- `s.dept = dis.readUTF();`
34+
- `s.avg = dis.readFloat();`
35+
Agar order change karoge to `EOFException` ya corrupted data milega.
36+
37+
3. Streams ka use
38+
- `FileInputStream` → raw bytes read karta hai.
39+
- `DataInputStream` → high-level methods deta hai (readInt, readUTF, readFloat).
40+
41+
4. Close order
42+
- Always `dis.close();` phir `fis.close();` (ya try-with-resources use karna best hai).
43+
44+
5. Serialization vs Data Streams
45+
- Yaha tumne `DataInputStream` use kiya hai → manually field by field write & read karna hota hai.
46+
- Agar tum `ObjectInputStream` use karte ho → pura object ek shot me read/write ho sakta hai
47+
(provided class implements Serializable).
48+
49+
- Agar tumne `DataOutputStream` se likha hai:
50+
```java
51+
dos.writeInt(rollno);
52+
dos.writeUTF(name);
53+
dos.writeUTF(dept);
54+
dos.writeFloat(avg);
55+
*/

0 commit comments

Comments
 (0)