Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I wrote this Java program for an assignment. package serialization; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import

I wrote this Java program for an assignment.

package serialization;

import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List;

public class StudentInformationSystems {

public static void main(String[] args) { // instantiates student with param constructor Student s = new Student("Name:David Ross",+1001); Student s1 = new Student("Name:Martin Jones", 1002); Student s2 = new Student("Name:Lena Asare", 1003); //creates list which stores student objects List list = new ArrayList<>(); list.add(s); list.add(s1); list.add(s2); //invoking write File method to write into a file writeFile(list); //invoke read file method to read from a file readFile();

}

//using serialisation to write objects into file public static void writeFile(List studentList) { // using try with resources for closing resources implicitly after program // execution // creates the object of FileOutStream and specify the path where you want to store student details // creates the object of ObjectOutputStream try (FileOutputStream fos = new FileOutputStream("E:\\NewSample.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos)) { // Writes an object to a file oos.writeObject(studentList); System.out.println("student records written to file successfully");

} catch (IOException e) { e.printStackTrace(); } }

//method for deserialization to read objects from a file @SuppressWarnings("unchecked") public static void readFile() {

//creates list which stores Student Objects List studentList = new ArrayList(); /*creates an instance of FileInputStream and give the path as argument from where you want to read data*/ //creates the instance of ObjectInputStream try (FileInputStream fis = new FileInputStream("E:\\NewSample.txt"); ObjectInputStream ois = new ObjectInputStream(fis) ) { //reads all objects from file and stores in a list studentList = (List) ois.readObject(); //traversing through list to access student details for(Student s : studentList) { System.out.println(s); } System.out.println("data read successfully"); } catch (IOException ioe) { ioe.printStackTrace(); System.err.println("ERROR: " + ioe.getMessage()); } catch (ClassNotFoundException cnfe) { System.err.println("ERROR: " + cnfe.getMessage()); }

}

}

//class which need to be serialized must implement Serializable interface class Student implements Serializable {

/*serialVersionUID will be used to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization*/ private static final long serialVersionUID = 1L; //add more attributes according to scenario and change parameterized constructor accordingly private String studentName; private int studentId;

public Student(String studentName, int studentId) { this.studentName = studentName; this.studentId = studentId; } public static long getSerialversionuid() { return serialVersionUID; }

public String getStudentName() { return studentName; }

public int getStudentId() { return studentId; }

@Override public String toString() { // TODO Auto-generated method stub return studentName+" "+studentId;

}

}

The output in Notepad is shown below;

sr java.util.ArrayListxa I sizexp w sr serialization.Student I studentIdL studentNamet Ljava/lang/String;xp t Name:David Rosssq ~ t Name:Martin Jonessq ~ t Name:Lena Asarex

Please can you ammend my program to produce the correct format:

Name David Ross

Name Martin Jones

Name Linda Asari,

In Notepad.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke

1st Edition

0130086509, 978-0130086501

More Books

Students also viewed these Databases questions