Java and the Magic of Serialization

skillfulwolverineSoftware and s/w Development

Dec 2, 2013 (3 years and 7 months ago)

53 views


Object
-
Oriented Programming in Java


The Magic of Serialization

Java and t
he Magic of Serialization

Class
StudentArrayTest

import

java.io.FileInputStream;

import

java.io.FileOutputStream;

import

java.io.ObjectInputStream;

import

java.io.ObjectOutputStream;


public

class

StudentArrayTest {



public

static

void

main(String[] args) {



// when program execution begins, determine the mode:



// 1:Build array of references to Student; Create new Student objects; S
erialize collection to disk



// 2:Restore the set of Student objects (with all the linking structure)
from disk



switch

(Input.
getInt
(
"1:Build
-
Display
-
Save 2:Load
-
Display"
, 1, 2)) {




case

1: {

// c
ase 1: Builds a set of Student objects then saves them as serialized data to a disk
-
based file




// Ask the user for the number of Student objects to be
built




// Use
int

value to create a new array of references to Student objects (
which start with

null

values
)




Student[] asSetOfStudents =
new

Student[Input.
getInt
(
"Number of Students:"
, 1, 10000)];





// On each iteration of the loop, a single
Student object is created and initialized.




for

(
int

i = 0; i < asSetOfStudents.
length
; ++i) {





// Create Student object, establish the First and Last names.





asSetOfStudents[i] =
new

Student(Input.
getString
(
"First:"
), Input.
getString
(
"Last:"
));





// Establish the courses (a variable number of courses) and grade for each course





asSetOfStudents[i].setGrades();




}





// Display Student objects to confirm that they were created and initialized correctly




for

(
int

i = 0; i < asSetOfStudents.
l
ength
; ++i)





System.
out
.println(asSetOfStudents[i]);





// Serialized output starts here.




try

{

// Using a try block in case there is a file I/O error





// Two related objects: one of type FileOutputStream which is built and sent to
ObjectOutputStream





ObjectOutputStream out =
new

ObjectOutputStream(
new

FileOutputStream(
"ArrayOfStudents.ser"
));






// Magic starts here ********************************************
*****************
**********************





// Entire collection of
Student objects written to disk with all structural information embedded in file





out.writeObject(asSetOfStudents);





// Magic ends here ***************************************************************
*****************
*****






out.close();




}
catch

(Exception e) {





e.printStackTrace();




}



}


break
;
// end case 1




case

2: {

// case 2: Loads array of serialized data stored earlier




try

{

// Using a try block in case there is a file I/O error





// There are two related objects: one of type
FileInputStream which is built and sent to ObjectInputStream





ObjectInputStream in =
new

ObjectInputStream(
new

FileInputStream(
"ArrayOfStudents.ser"
));






// Magic starts here *****************************************************************
****************
*





// Entire collection of Student objects
read from disk using

the structural information
stored in
file





Student[] asDiskedBasedSetOfStudents = (Student[]) in.readObject();





// Magic ends here ************************************
***********
****************
*********************






for

(
int

i = 0; i < asDiskedBasedSetOfStudents.
length
; ++i)






System.
out
.println(asDiskedBasedSetOfStudents[i]);






in.close();




}
catch

(Exception e) {





e.printStackTrace();




}



}

break
;
// end case 2



}
// end switch


}
// end main()

}
// end class StudentTest



Page
2

of
2


Object
-
Oriented Programming in Java


The Magic of Serialization

Class Student

import

java.io.Serializable;


@SuppressWarnings

(
"serial"
)

public

class

Student
implements

Serializable {


private

String
sLastName
;


private

String
sFirstName
;


private

CourseRecord[]
acrCourseInformation
;



public

Student() {


}



public

Student(String sLastName, String sFirstName) {



this
.
sLastName

= sLastName;



this
.
sFirstName

= sFirstName;


}
// end constructor



public

void

setGrades() {



int

nNumCourses = Input.
getInt
(
"Number of Courses:"
, 0, 10);



acrCourseInformation

=
new

CourseRecord[nNumCourses];



for

(
int

i = 0; i < nNumCourses; ++i) {




acrCourseInformation
[i] =
new

CourseRecord();



}


}
// end setGrades()



public

String toString() {



String sAssembledInformation =
sLastName

+
", "

+
sFirstName

+
": "
;



for

(
int

i = 0; i <
acrCourseInformation
.
length
; ++i)




sAssembledInformation +=
acrCourseInformation
[i] +
" "
;



return

sAssembledInformation;


}
// end toStrin
g()

}
// end class Student

Class CourseRecord

import

java.io.Serializable;


@SuppressWarnings

(
"serial"
)

public

class

CourseRecord
implements

Serializable {


private

String
sCourseName
;


private

int

nGrade
;



public

CourseRecord() {



sCourseName

= Input.
getString
(
"Course Name:"
);



nGrade

= Input.
getInt
(
"Grade: "
, 0, 100);


}
// end constructor



public

String toString() {



return

sCourseName

+
":"

+
nGrade
;


}
// end toString()

}
// end class CourseRecord