Skip to content

Commit 8298ff8

Browse files
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

2 files changed

+54
-7
lines changed

Section23JavaIOStreams/Serialisation Storing Data in a File/src/printID.java

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ public String toString() {
2424
}
2525

2626
class PrintID {
27-
/*public static void main(String[] args) throws Exception {
27+
/*
28+
public static void main(String[] args) throws Exception {
2829
Customer list[] = {
2930
new Customer("Smith", "123456789"),
3031
new Customer("Johnson", "1234567"),
3132
new Customer("Ajay", "12345345")
3233
};
3334
34-
FileOutputStream fos = new FileOutputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Customer.txt");
35+
FileOutputStream fos = new FileOutputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Customer.txt");
3536
ObjectOutputStream oos = new ObjectOutputStream(fos);
3637
3738
oos.writeInt(list.length); // Writing array length first
@@ -41,12 +42,13 @@ class PrintID {
4142
oos.close();
4243
fos.close();
4344
System.out.println("Customer data serialized successfully.");
44-
}*/
45+
}
46+
*/
4547

4648
public static void main(String[] args) throws Exception {
47-
java.util.Scanner sc =new java.util.Scanner(System.in);
49+
java.util.Scanner sc = new java.util.Scanner(System.in);
4850

49-
FileInputStream fis = new FileInputStream("C:\\Users\\somes\\Downloads\\JAVA SE\\Section23JavaIOStreams\\Serialisation Storing Data in a File\\MyJAVA\\Customer.txt");
51+
FileInputStream fis = new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/src/MyJAVA/Customer.txt");
5052
ObjectInputStream ois = new ObjectInputStream(fis);
5153

5254
int length = ois.readInt(); // Read-number of customers stored
@@ -56,15 +58,60 @@ public static void main(String[] args) throws Exception {
5658

5759
String name = sc.nextLine();
5860
for (int i = 0; i < length; i++) {
59-
customers[i] = ( Customer ) ois.readObject();
61+
customers[i] = (Customer) ois.readObject();
6062
}
6163

6264
ois.close();
6365
fis.close();
6466

6567
System.out.println("Deserialized Customer Data:");
68+
6669
for (Customer c : customers) {
6770
System.out.println(c);
6871
}
6972
}
70-
}
73+
}
74+
75+
/*
76+
1. Serializable Interface
77+
- `Customer implements Serializable` → ye batata hai ki Customer objects ko
78+
byte-stream me convert kiya ja sakta hai (serialize) aur wapas object me laaya ja sakta hai (deserialize).
79+
- Agar Serializable implement nahi karte to `NotSerializableException` aata.
80+
81+
2. Static field ka behaviour
82+
- `static int count` serialise nahi hota (kyunki static class level property hoti hai).
83+
- Jab object wapas deserialize hota hai, uska static field fresh hota hai (default class ke sath).
84+
- Is case me sirf `custID`, `name`, aur `phno` serialise hote hain.
85+
86+
3. ObjectOutputStream (Serialization)
87+
- `ObjectOutputStream oos = new ObjectOutputStream(fos)`
88+
- `oos.writeInt(list.length)` → pehle array ka length likh diya (taaki read karte waqt pata ho kitne objects hain).
89+
- `oos.writeObject(c)` → har Customer object file me serialize kiya gaya.
90+
91+
4. ObjectInputStream (Deserialization)
92+
- `int length = ois.readInt()` → pehle number of customers read kiya.
93+
- `customers[i] = (Customer) ois.readObject()` → file se ek ek object read karke array me store kiya.
94+
- `readObject()` ko typecast karna padta hai.
95+
96+
5. toString() Override
97+
- `Customer` class ne `toString()` override kiya hai → isliye print karne par directly readable string aata hai:
98+
```
99+
Customer ID: C1
100+
Name: Smith
101+
Phno: 123456789
102+
```
103+
104+
6. File Path
105+
- Serialization aur Deserialization dono ek hi file path use karte hain
106+
(`Customer.txt` binary file hoti hai, text jaisa readable nahi dikhega).
107+
108+
7. Scanner ka use
109+
- Deserialization code me tumne `Scanner` se ek name input liya hai,
110+
lekin uska use filtering ke liye nahi ho raha. Agar chaho to loop me
111+
input name se match karke sirf wahi customer print kar sakte ho.
112+
113+
- `try-with-resources` use karo taaki streams automatically close ho jaye.
114+
- `serialVersionUID` add karo class me (recommended for Serializable classes).
115+
```java
116+
private static final long serialVersionUID = 1L;
117+
*/
174 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)