Commit 26e4e7c
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- Section23JavaIOStreams/Serialisation Storing Data in a File/src
1 file changed
+45
-14
lines changedLines changed: 45 additions & 14 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
| 6 | + | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
10 | 14 | | |
11 | | - | |
12 | | - | |
13 | | - | |
14 | | - | |
15 | | - | |
16 | | - | |
17 | 15 | | |
18 | 16 | | |
19 | 17 | | |
| 18 | + | |
20 | 19 | | |
21 | | - | |
22 | | - | |
| 20 | + | |
| 21 | + | |
23 | 22 | | |
24 | | - | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
0 commit comments