Commit 7fb5f35
committed
feat: implement DataOutputStreamsExample22 for writing student data in binary format
WHAT:
- Added `DataOutputStreamsExample22` class to demonstrate serialization using `DataOutputStream`.
- Writes a student's details (roll number, name, department, average marks) into a binary file `Student2.txt`.
HOW:
- Used try-with-resources to safely handle `FileOutputStream` and `DataOutputStream`.
- Serialized fields in the following order:
1. `writeInt(rollno)`
2. `writeUTF(name)`
3. `writeUTF(dept)`
4. `writeFloat(avg)`
- Ensures the same order must be followed during deserialization.
WHY:
- Demonstrates **low-level manual serialization** of Java primitives and Strings.
- Provides a platform-independent binary representation of structured data.
- Shows safe resource management using Java’s try-with-resources.
REAL-WORLD APPLICATIONS:
- Storing student records, employee details, or configuration data in a lightweight binary format.
- Used in games and simulations for saving state efficiently.
- Provides a base for building custom serialization frameworks.
- Faster and more compact than plain text storage.
BENEFITS:
- Auto-closing of streams avoids memory/resource leaks.
- Compact binary format ensures efficiency in storage and retrieval.
- Maintains data integrity across JVMs and platforms.
NEXT IMPROVEMENTS:
- Add a matching `DataInputStreamsExample22` to deserialize and read back student data.
- Wrap serialization logic into reusable methods (e.g., `writeStudent(Student student)`).
- Consider using `Serializable` interface for more scalable object persistence.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2015cf0 commit 7fb5f35
File tree
1 file changed
+75
-0
lines changed- Section23JavaIOStreams/Serialisation Storing Data in a File/src
1 file changed
+75
-0
lines changedLines changed: 75 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 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
0 commit comments