Worksheet - Java - (22bcs50102) 3
Worksheet - Java - (22bcs50102) 3
WORKSHEET 3
1. Aim: Write a program to define a class product with attributes name ,price and quantity .Create
method for serialization and deserialization also demonstrate saving the product object to file
and then loading it back to ensure the data integrity during the process.
2. Source Code:
import java.util.*;
import java.io.*;
class Product implements Serializable{
String name ;
int price ;
int quantity ;
public Product(String name, int price, int quantity) {
this.name = name;
this.price = price;
this.quantity =quantity;
}
}
class Serializeproduct
{
void serialize(){
try{
catch(Exception e){System.out.println(e);}
}
void deserialize(){
try{
}
public class Main
{
public static void main(String args[])
{ Serializeproduct s1 = new Serializeproduct();
s1.serialize();
s1.deserialize();}}
3. Screenshot of Outputs:
Java.txt file
4. Learning Outcomes :
I. I gained knowledge of how to serialise and deserialize a class object, which is useful for
transferring data between several Java applications or even across networks.
II. How to store and load objects from a file using Java serialisation.
III. I gained knowledge on how to employ try and catch blocks in a programme.
IV. Exception handling is a smart practise to make sure your programme can gracefully manage
unanticipated problems.
V. I gained knowledge on how to use input and output streams.
VI. Comprehending file paths and using files in Java.