Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

write and submit VehicleSerializable.java The vehicle serializable will be a code similar to the following: // Java code for serialization and deserialization // of a

write and submit VehicleSerializable.java

The vehicle serializable will be a code similar to the following:

// Java code for serialization and deserialization // of a Java object import java.io.*; class Demo implements java.io.Serializable { public int a; public String b; // Default constructor public Demo(int a, String b) { this.a = a; this.b = b; } } class Test { public static void main(String[] args) { Demo object = new Demo(1, "geeksforgeeks"); String filename = "file.ser"; // 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(object); out.close(); file.close(); System.out.println("Object has been serialized"); } catch(IOException ex) { System.out.println("IOException is caught"); } Demo object1 = null; // Deserialization try { // Reading the object from a file FileInputStream file = new FileInputStream(filename); ObjectInputStream in = new ObjectInputStream(file); // Method for deserialization of object object1 = (Demo)in.readObject(); in.close(); file.close(); System.out.println("Object has been deserialized "); System.out.println("a = " + object1.a); System.out.println("b = " + object1.b); } catch(IOException ex) { System.out.println("IOException is caught"); } catch(ClassNotFoundException ex) { System.out.println("ClassNotFoundException is caught"); } } }

copy and submit fig 17.18 ReadSequentialFile.java, with the following modifications: (1) change it to work with VehicleSerializable.java (2) change lines 43-45 to instead call VehicleSerializable display() method. (3) The input file name on line 21 will be vehicle.ser

Code below needs to be modified with the democode above so that they work together.

1 // Fig. 17.18: ReadSequentialFile.java 2 // Reading a file of objects sequentially with ObjectInputStream 3 // and displaying each record. 4

5

6

7

8

9

10 11 public class ReadSequentialFile 12 { 13 private ObjectInputStream input; 14 15 // enable user to select file to open 16 public void openFile() 17 { 18 try // open file 19 { 20 input = new ObjectInputStream( 21 new FileInputStream( "clients.ser" ) ); 22 } // end try 23 catch ( IOException ioException ) 24 { 25 System.err.println( "Error opening file." ); 26 } // end catch 27 } // end method openFile 28 29 // read record from file 30 public void readRecords() 31 { 32 AccountRecordSerializable record; 33 System.out.printf( "%-10s%-12s%-12s%10s ", "Account", 34 "First Name", "Last Name", "Balance" ); 35 36 try // input the values from the file 37 {

38 while ( true ) 39 { 40 41 42 // display record contents 43 System.out.printf( "%-10d%-12s%-12s%10.2f ", 44 record.getAccount(), record.getFirstName(), 45 record.getLastName(), record.getBalance() ); 46 } // end while 47 } // end try 48 catch ( EOFException endOfFileException ) 49 { 50 return; // end of file was reached 51 } // end catch 52 catch ( ClassNotFoundException classNotFoundException ) 53 { 54 System.err.println( "Unable to create object." ); 55 } // end catch 56 catch ( IOException ioException ) 57 { 58 System.err.println( "Error during read from file." ); 59 } // end catch 60 } // end method readRecords 61 62 // close file and terminate application 63 public void closeFile() 64 { 65 try // close file and exit 66 { 67 if ( input != null ) 68 input.close(); 69 } // end try 70 catch ( IOException ioException ) 71 { 72 System.err.println( "Error closing file." ); 73 System.exit( 1 ); 74 } // end catch 75 } // end method closeFile 76 } // end class ReadSequentialFile

The code below tests both programs above.

1 // Fig. 17.19: ReadSequentialFileTest.java 2 // Testing class ReadSequentialFile. 3

4 public class ReadSequentialFileTest 5 { 6 public static void main( String[] args ) 7 { 8 ReadSequentialFile application = new ReadSequentialFile(); 9 10 application.openFile(); 11 application.readRecords(); 12 application.closeFile(); 13 } // end main 14 } // end class ReadSequentialFileTest

Thank you

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 Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions