Commit 8298ff8
committed
feat: implement Customer serialization and deserialization using Object Streams
WHAT:
- Created a `Customer` class implementing `Serializable` with fields:
- `custID` (auto-generated unique ID using static counter),
- `name`,
- `phno`.
- Implemented serialization (`ObjectOutputStream`) to store multiple customers in a file.
- Implemented deserialization (`ObjectInputStream`) to retrieve and display customers from file.
HOW:
- Serialization:
- Write the number of customers (`list.length`) first.
- Serialize each `Customer` object with `writeObject()`.
- Deserialization:
- Read count using `readInt()`.
- Read and cast objects back to `Customer[]`.
- Used `toString()` for clean formatted customer display.
WHY:
- Demonstrates how to persist Java objects (with state) into a file.
- Ensures objects can be reconstructed later without manual parsing.
- Useful for lightweight persistence when databases are unnecessary.
REAL-WORLD APPLICATIONS:
- Saving customer/user profiles in desktop applications.
- Temporary caching of session data in serialized form.
- Quick prototyping of object persistence without DB integration.
NOTES:
- `static` fields (like `count`) are not serialized; IDs regenerate with each run.
- Must maintain same class definition when deserializing to avoid `InvalidClassException`.
NEXT IMPROVEMENTS:
- Add `serialVersionUID` for better version control during deserialization.
- Improve uniqueness of `custID` across multiple runs (e.g., UUID instead of static counter).
- Enhance program with search functionality (filter by name/ID).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 5890a7a commit 8298ff8
File tree
2 files changed
+54
-7
lines changed- Section23JavaIOStreams
- Serialisation Storing Data in a File/src
- src/MyJAVA
2 files changed
+54
-7
lines changedLines changed: 54 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
27 | | - | |
| 27 | + | |
| 28 | + | |
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | | - | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| |||
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | | - | |
| 45 | + | |
| 46 | + | |
45 | 47 | | |
46 | 48 | | |
47 | | - | |
| 49 | + | |
48 | 50 | | |
49 | | - | |
| 51 | + | |
50 | 52 | | |
51 | 53 | | |
52 | 54 | | |
| |||
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
59 | | - | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
63 | 65 | | |
64 | 66 | | |
65 | 67 | | |
| 68 | + | |
66 | 69 | | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
70 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
Binary file not shown.
0 commit comments