Question
I wrote the program below for a student Information System, using serialization and deserialization. It outputs one student record. Please can you help me by
I wrote the program below for a student Information System, using serialization and deserialization. It outputs one student record. Please can you help me by ammending this program, so that I can add more student records. Can the answer be typewritten.
import java.io.*; import java.util.*;
import serialization.Student;
public class Student implements Serializable { // Setting the serialVersionUID private static final long serialVersionUID = 1L;
public static final String fileName = null;
// The data that belongs to the class Student private String studentID, firstName, lastName, dob, course, enrollmentDate; // constructor for the class Student public Student(String studentID, String firstName, String lastName, String dob, String course, String enrollmentDate) { //this.studentID refers to object studentID in the current class this.studentID = studentID; this.firstName = firstName; this.lastName = lastName; this.dob = dob; this.course = course; this.enrollmentDate = enrollmentDate; } public Student() { }// Creates a String representation of an object public String toStriing() { //object passed to function return(studentID +" "+ firstName +" "+ lastName +" "+ dob +" "+ course +" "+ enrollmentDate); } public static class JavaSeriialization{
public static void main(String[] args) { // An object of the type Student initialized Student student = new Student("P101"," Jon","Doe","11/11/68", "APC", "12/12/19"); // A file set for writing down the serialized/deserialized dada String fileneme = ("E:\\SerialFile.txt"); // Serialization try { // FileOutputStream connected to the file to write content FileOutputStream file = new FileOutputStream("E:\\Desrialize.txt"); // ObjectOutputStream connected to the FileOutputStream to write the Object to the file ObjectOutputStream obj = new ObjectOutputStream(file); // writeObject(object) writes the serialized data to the file obj.writeObject(student); System.out.println("Serialization completed"); obj.close(); } catch(IOException e) { e.printStackTrace(); }// deserializtion student = null; try { // FileInputStream is connected to the file to read content FileInputStream file = new FileInputStream("E:\\Deserialize1.txt"); // ObjectInputStream connected to the FileInputStream to read the serialized data from the file ObjectInputStream obj = new ObjectInputStream(file); // readOject() reads the serialized data and deserilizes it to the object obj1 student = (Student)obj.readObject(); obj.close(); file.close(); // Printing the deserialized object System.out.println(" Deserialized objects data "); System.out.println(student); // Writing the deserialized data to the file FileOutputStream fs = new FileOutputStream("E:\\Deserialize.txt"); String stu = student.toStriing(); byte[] bytesArray = stu.getBytes(); fs.write(bytesArray); fs.close(); } catch(IOException e) { e.printStackTrace(); } catch(ClassNotFoundException e) { e.printStackTrace(); } }
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started