1. deserialize a Java object that contains data about a movie. The serialized ob
ID: 3716715 • Letter: 1
Question
1. deserialize a Java object that contains data about a movie. The serialized object contains a String containing the movie's title, a String containing the name of the movie's director, and an int containing the year the movie was released. 2. create a class that contains a main method that deserializes this file and displays the title, director, and year using the Movie class's getter methods. package lab6; import java.io.Serializable; public class Movie implements Serializable { private String title; private String director; private int year; public Movie(String title, String director, int year) { this.title = title; this.director = director; this.year = year; } public String getTitle() { return title; } public String getDirector() { return director; } public int getYear() { return year; } }
Explanation / Answer
Movie.java
import java.io.Serializable;
public class Movie implements Serializable {
private String title;
private String director;
private int year;
public Movie(String title, String director, int year) {
this.title = title;
this.director = director;
this.year = year;
}
public String getTitle() {
return title;
}
public String getDirector() {
return director;
}
public int getYear() {
return year;
}
}
________________
Test.java
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Movie m1 = null;
String filename = "SerialFile.ser";
String title,director;
int year;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Movie title :");
title=sc.nextLine();
System.out.print("Enter the nam eof Director :");
director=sc.nextLine();
System.out.print("Enter the Year :");
year=sc.nextInt();
Movie m = new Movie(title,director,year);
// Serialization
try
{
//Saving of object in a file
FileOutputStream file = new FileOutputStream(filename);
ObjectOutputStream out = new ObjectOutputStream(file);
// Method for serialization of object
out.writeObject(m);
out.close();
file.close();
System.out.println("Movie Object has been serialized");
}
catch(IOException ex)
{
System.out.println("IOException is caught");
}
// Deserialization
try {
// Reading the object from a file
FileInputStream file = new FileInputStream(filename);
ObjectInputStream in = new ObjectInputStream(file);
// Method for deserialization of object
m1 = (Movie) in.readObject();
in.close();
file.close();
System.out.println("Movie Object has been deserialized ");
System.out.println("Movie Name = " + m1.getTitle());
System.out.println("Director = " + m1.getDirector());
System.out.println("Year = " + m1.getYear());
}
catch (IOException ex) {
System.out.println("IOException is caught");
}
catch (ClassNotFoundException ex) {
System.out.println("ClassNotFoundException is caught");
}
}
}
__________________
Output:
Enter the Movie title :Titanic
Enter the nam eof Director :James Cameron
Enter the Year :1996
Movie Object has been serialized
Movie Object has been deserialized
Movie Name = Titanic
Director = James Cameron
Year = 1996
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.