Commit 2015cf0
committed
feat: add DataOutputStream example for serializing Student object
WHAT:
- Implemented `DataOutputStreamsExample` class.
- Created `Student11` class with fields: rollno, name, avg, dept.
- Used `DataOutputStream` with `FileOutputStream` to write student data into a file.
HOW:
1. Instantiated a `Student11` object and populated sample data.
2. Opened `FileOutputStream` pointing to `Student1.txt`.
3. Wrapped it with `DataOutputStream` for writing Java primitives in binary.
4. Wrote fields in a specific order:
- `writeInt()` → roll number
- `writeUTF()` → name
- `writeUTF()` → department
- `writeFloat()` → average
5. Closed streams safely after writing.
6. Data is saved in **binary format**, not human-readable text.
WHY:
- Demonstrates **manual serialization** using `DataOutputStream`.
- Ensures data is written in a **platform-independent binary format**.
- Reading must follow the **exact same order** using `DataInputStream`.
REAL-WORLD APPLICATIONS:
- Saving student records or user profiles in lightweight storage.
- Writing config/state data in binary (faster than plain text parsing).
- Efficient persistence in games, finance apps, or IoT systems.
- Forms the basis of custom serialization frameworks.
BENEFITS:
- Compact and efficient storage (binary instead of text).
- Machine-independent format due to `DataOutputStream`.
- Works with Java primitive types and UTF-encoded Strings.
NEXT IMPROVEMENTS:
- Add matching `DataInputStreamsExample` to read and reconstruct `Student11`.
- Refactor into reusable `writeStudent(Student11 student, File file)` method.
- Add try-with-resources for cleaner resource management.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 7949c9e commit 2015cf0
File tree
2 files changed
+2
-3
lines changed- Section23JavaIOStreams
- Serialisation Storing Data in a File/src
- src/MyJAVA
2 files changed
+2
-3
lines changedLines changed: 2 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
12 | 11 | | |
13 | 12 | | |
14 | 13 | | |
15 | | - | |
| 14 | + | |
16 | 15 | | |
17 | 16 | | |
18 | 17 | | |
| |||
Binary file not shown.
0 commit comments