0% found this document useful (0 votes)
17 views3 pages

Worksheet - Java - (22bcs50102) 3

The document describes a Java program that defines a Product class with name, price, and quantity attributes. It implements serialization and deserialization methods to save Product objects to a file and load them back. The program creates a Product instance, serializes it to a file, then deserializes and prints the object's attributes, demonstrating how to preserve data integrity during the serialization and deserialization process.

Uploaded by

Hrishabh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Worksheet - Java - (22bcs50102) 3

The document describes a Java program that defines a Product class with name, price, and quantity attributes. It implements serialization and deserialization methods to save Product objects to a file and load them back. The program creates a Product instance, serializes it to a file, then deserializes and prints the object's attributes, demonstrating how to preserve data integrity during the serialization and deserialization process.

Uploaded by

Hrishabh Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

COMPUTER SCIENCE & ENGINEERING

WORKSHEET 3

Student Name: Hrishabh Gupta UID: 22BCS50102


Branch: CSE Section/Group: 902 -B
Semester: 3rd Date of Performance: 17-10-2023
Subject Name: Java Programming Subject Code: 22CSH-201

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{

Product p1 =new Product("coldsake",50 ,1);

FileOutputStream fout=new FileOutputStream("java.txt");


ObjectOutputStream out=new ObjectOutputStream(fout);
out.writeObject(p1);
out.close();
System.out.println("successfuly convert file into object");
}

catch(Exception e){System.out.println(e);}
}
void deserialize(){
try{

ObjectInputStream in=new ObjectInputStream(new FileInputStream("java.txt"));


Product p=(Product)in.readObject();

System.out.println(p.name+" "+p.price+" "+p.quantity);


System.out.println("successfuly convert object into file");
in.close();
}catch(Exception e){System.out.println(e);} }

}
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.

You might also like