Commit 4b77d1b
committed
feat: add DataInputStream-based deserialization with Student1 example
WHAT:
- Created `Student1` class with fields: rollno, name, dept, avg.
- Implemented `DataInputStreamsExample2` to read student data from file
`Student2.txt` using `DataInputStream`.
HOW:
1. Opened file via `FileInputStream` and wrapped in `DataInputStream`.
2. Instantiated `Student1` object.
3. Read data in the same sequence as written:
- readInt() → rollno
- readUTF() → name
- readUTF() → dept
- readFloat() → avg
4. Printed student details to the console.
5. Properly closed the input stream.
KEY POINTS:
- **Order matters:** Data must be read in the same order and type in
which it was written, otherwise runtime errors occur.
- `DataInputStream` simplifies reading Java primitives and strings
without manual parsing.
- Complements `DataOutputStream` for serialization.
REAL-WORLD APPLICATIONS:
- Custom serialization format for performance-sensitive apps.
- Storing and retrieving structured student or employee records.
- Lightweight persistence layer for local file-based storage.
- Useful in games, logging, or legacy system integrations.
IMPROVEMENTS:
- Use try-with-resources for auto-closing streams.
- Add validation/error handling if file data is corrupted.
- Add corresponding `DataOutputStream` writer to complete the cycle.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7ab1469 commit 4b77d1b
File tree
2 files changed
+33
-0
lines changed- Section23JavaIOStreams
- Serialisation Storing Data in a File/src
- src/MyJAVA
2 files changed
+33
-0
lines changedLines changed: 33 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
Binary file not shown.
0 commit comments