Professional Documents
Culture Documents
WORKSHEET 3
1. Aim: Write a program to define a class product with attributes name price and quality.
Create method for serialization and deserialization also demonstrate saving the product object
to a file and then loading it back?
2. Source Code:
import java.io.*;
String name;
int price;
String quality;
p1.name ="Bottle";
p1.price=200;
p1.quality="good";
String filename="C:\\Users\\dell\\Desktop\\conduct.txt";
try{
DEPRTMENT OF
COMPUTER SCIENCE & ENGINEERING
FileOutputStream fos = new FileOutputStream(filename);
oos.writeObject(p1);
oos.close();
fos.close();
catch(Exception e){
System.out.println("Exception:"+ e);
try{
ois.close();
fis.close();
System.out.println("Object is here and its name and price and quality is: "+obj.name +"
"+ obj.price +" "+ obj.quality);
catch(Exception e){
System.out.println("Exception:"+ e);
}
DEPRTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Screenshot of Outputs:
4. Learning Outcomes
I. How to use Java serialization to save and load objects from a file.
II. I learnt how to use try and catch blocks in a code.
III. Exception Handling which is a good practice to ensure that your program can
handle unexpected errors gracefully.
IV. Understanding file paths and how to work with files in Java.