0% found this document useful (0 votes)
8 views23 pages

Serialization Assignment

The document contains Java code demonstrating serialization and deserialization of various classes, including Customer, Person, Student, and Car. It illustrates how to write and read objects to and from files using ObjectOutputStream and ObjectInputStream. Additionally, it includes an example of using Externalizable for custom serialization of a Student class.
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)
8 views23 pages

Serialization Assignment

The document contains Java code demonstrating serialization and deserialization of various classes, including Customer, Person, Student, and Car. It illustrates how to write and read objects to and from files using ObjectOutputStream and ObjectInputStream. Additionally, it includes an example of using Externalizable for custom serialization of a Student class.
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

import [Link].

*;

class Customer implements Serializable {

private static final long serialVersionUID = 1L;

private int custid;

private String custname;

private String address;

private int age;

public Customer(int custid, String custname, String address, int age) {

[Link] = custid;

[Link] = custname;
[Link] = address;

[Link] = age;

public int getCustid() {

return custid;

public String getCustname() {

return custname;

public String getAddress() {

return address;
}

public int getAge() {

return age;

public class Ques1FileHandling {

public static void main(String[] args) {

// Create a Customer object

Customer c = new Customer(101, "Tanmay", "Andheri, Mumbai", 22);

try (FileOutputStream fos = new FileOutputStream("D:\\FileHandling\\Que1")) {

try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {

[Link](c);
}

} catch (Exception e) {

[Link]();

[Link]("Before Serialization:");

[Link]("Name: " + [Link]() + " | ID: " + [Link]() + " | Address: " +
[Link]()

+ " | Age: " + [Link]());

c = null;

try (FileInputStream fis = new FileInputStream("D:\\FileHandling\\Que1")) {

try (ObjectInputStream ois = new ObjectInputStream(fis)) {

Customer c1 = (Customer) [Link]();

[Link]("\nAfter Deserialization:");
[Link]("Name: " + [Link]() + " | ID: " + [Link]() + " |
Address: "

+ [Link]() + " | Age: " + [Link]());

} catch (Exception e) {

[Link]();

Q2 )

package que2;

import [Link];

import [Link];

import [Link];

import [Link];
import [Link];

import [Link].*;

class Person implements Serializable {

private String name;

private int age;

public Person(String name, int age) {

super();

[Link] = name;

[Link] = age;

public String getName() {

return name;
}

public void setName(String name) {

[Link] = name;

public int getAge() {

return age;

public void setAge(int age) {

[Link] = age;

}
class Student extends Person {

private int rollno;

public int getRollno() {

return rollno;

public void setRollno(int rollno) {

[Link] = rollno;

public Student(String name, int age, int rollno) {

super(name, age);

[Link] = rollno;

}
}

public class Demo {

public static void main(String[] args) {

Student s = new Student("Tanmay", 19, 85);

try (FileOutputStream fos = new FileOutputStream("D:\\FileHandling\\[Link]")) {

try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {

[Link](s);

} catch (Exception e) {

[Link]();

}
[Link]("Before Serialization:");

[Link]("Name: "+[Link]()+" Age: "+[Link]()+" Roll no: "+[Link]());

s=null;

try (FileInputStream fis = new FileInputStream("D:\\FileHandling\\[Link]")) {

try (ObjectInputStream ois = new ObjectInputStream(fis)) {

Student s1 = (Student) [Link]();

[Link]("\nAfter Deserialization:");

[Link]("Name: "+[Link]()+" Age: "+[Link]()+" Roll no:


"+[Link]());

} catch (Exception e) {

[Link]();

}
}

Q3)

package studentSerializationDemo;

import [Link].*;

class Student1 implements Serializable {

private static final long serialVersionUID = 1L;

private int rollno;

private String name;

private int age;

public Student1(int rollno, String name, int age) {

[Link] = rollno;

[Link] = name;

[Link] = age;

public int getRollno() {

return rollno;

public void setRollno(int rollno) {

[Link] = rollno;

}
public String getName() {

return name;

public void setName(String name) {

[Link] = name;

public int getAge() {

return age;

public void setAge(int age) {

[Link] = age;

@Override

public String toString() {

return "Student1 [RollNo=" + rollno + ", Name=" + name + ", Age=" + age + "]";

public class StudentSerializationDemo {

public static void main(String[] args) {

Student1 s1 = new Student1(101, "Devdatta", 25);

Student1 s2 = new Student1(102, "Rahul", 22);

try (FileOutputStream fos = new FileOutputStream("D:\\FileHandling\\[Link]")) {

try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {


[Link](s1);

[Link](s2);

[Link]("Serialization done! Two Student1 objects written.");

} catch (Exception e) {

[Link]();

} catch (Exception e) {

[Link]();

s1 = null;

s2 = null;

try (FileInputStream fis = new FileInputStream("D:\\FileHandling\\[Link]")) {

try (ObjectInputStream ois = new ObjectInputStream(fis)) {

Student1 st1 = (Student1) [Link]();

Student1 st2 = (Student1) [Link]();

[Link]("Deserialization done!");

[Link](st1);

[Link](st2);

} catch (Exception e) {

[Link]();

} catch (Exception e) {

[Link]();
}

Q4)

package que4;

import [Link].*;

class Engine implements Serializable {

private static final long serialVersionUID = 1L;

private int speed;

private String type;

public Engine(int speed, String type) {

[Link] = speed;

[Link] = type;

public int getSpeed() {

return speed;

public void setSpeed(int speed) {

[Link] = speed;

public String getType() {

return type;
}

public void setType(String type) {

[Link] = type;

@Override

public String toString() {

return "Engine [Speed=" + speed + ", Type=" + type + "]";

class Car implements Serializable {

private static final long serialVersionUID = 1L;

private Engine engine;

private String modelname;

public Engine getEngine() {

return engine;

public void setEngine(Engine engine) {

[Link] = engine;

public String getModelname() {

return modelname;

public void setModelname(String modelname) {

[Link] = modelname;

}
@Override

public String toString() {

return "Car [Model=" + modelname + ", " + engine + "]";

public class CarSerializationDemo {

public static void main(String[] args) {

Engine e = new Engine(200, "Petrol");

Car c = new Car();

[Link]("Honda City");

[Link](e);

try (FileOutputStream fos = new FileOutputStream("D:\\FileHandling\\[Link]");

ObjectOutputStream oos = new ObjectOutputStream(fos)) {

[Link](c);

[Link]("Serialization done!");

} catch (Exception ex) {

[Link]();

c = null;

try (FileInputStream fis = new FileInputStream("D:\\FileHandling\\[Link]");

ObjectInputStream ois = new ObjectInputStream(fis)) {

Car c1 = (Car) [Link]();

[Link]("Deserialization done!");
[Link](c1);

} catch (Exception ex) {

[Link]();

Q5}

package que5;

import [Link].*;

class Student implements Externalizable {

private int rollno;

private String name;

private String qualification;

private String hobbies;

private String bloodgroup;

private String emailid;


public Student() {}

public Student(int rollno, String name, String qualification,

String hobbies, String bloodgroup, String emailid) {

[Link] = rollno;

[Link] = name;

[Link] = qualification;

[Link] = hobbies;

[Link] = bloodgroup;

[Link] = emailid;

public int getRollno() { return rollno; }


public void setRollno(int rollno) { [Link] = rollno; }

public String getName() { return name; }

public void setName(String name) { [Link] = name; }

public String getQualification() { return qualification; }

public void setQualification(String qualification) { [Link] = qualification; }

public String getHobbies() { return hobbies; }

public void setHobbies(String hobbies) { [Link] = hobbies; }

public String getBloodgroup() { return bloodgroup; }

public void setBloodgroup(String bloodgroup) { [Link] = bloodgroup; }

public String getEmailid() { return emailid; }


public void setEmailid(String emailid) { [Link] = emailid; }

// @Override

public String toString() {

return "Student [RollNo=" + rollno + ", Name=" + name + ", Qualification=" + qualification +

", Hobbies=" + hobbies + ", BloodGroup=" + bloodgroup + ", Email=" + emailid + "]";

@Override

public void writeExternal(ObjectOutput out) throws IOException {

[Link](rollno);

[Link](name);

[Link](qualification);

}
@Override

public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {

rollno = [Link]();

name = (String) [Link]();

qualification = (String) [Link]();

public class Que5 {

public static void main(String[] args) {

Student s = new Student(101, "Tanmay", "[Link]", "Coding", "A+", "tanmay@[Link]");

try (FileOutputStream fos = new FileOutputStream("D:\\FileHandling\\student_ext.txt");

ObjectOutputStream oos = new ObjectOutputStream(fos)) {


[Link](s);

[Link]("Student object serialized (only rollno, name, qualification stored).");

} catch (Exception e) {

[Link]();

try (FileInputStream fis = new FileInputStream("D:\\FileHandling\\student_ext.txt");

ObjectInputStream ois = new ObjectInputStream(fis)) {

Student deserialized = (Student) [Link]();

[Link]("Deserialized Student object:");

[Link](deserialized);

} catch (Exception e) {

[Link]();
}

You might also like