Previously we learned that some specific methods can be used to process int, double, and other types of data for input and output. Now suppose we have a student class. Each student object contains the name, language, mathematics, and English scores. The name is 'string' type and the score is 'int' type. What do you think of to save the data of the student object? Of course, the previous method can be used to output data to a file using writeint and other methods, and read data from the file using readint and other methods. But what if we encounter an object with more than ideological data ???
Object serialization is to directly process the entire object as input and output. Java provides a mechanism to complete this task. Its name is serialization. In short, it is to process the attributes in the object according to the stream method. Note that only attributes can be serialized. Because the attributes are variables, data can be stored, the method is program code, which makes no sense for output input processing.
As long as a class implements the serializable interface, the objects of this class can be serialized. The serializable interface is a mark interface, which means that no member is defined, simply put, it is an empty interface. The cloneable interface is also a blank interface when learning object replication. The class implementing the mark interface does not need to be changed internally (the interface has no way to implement it );
Next we will write a serializable class: (there is a problem here, the result is garbled in the file, but it does not affect getting data from the file)
Package ch21; import Java. io. *; // The student class implements the serializable interface, so its objects can be serially Input and Output public class student implements serializable {private string name; private int chscore, engscore; private transient int mathscore; // transient is used to modify the attribute public student (string N, int C, int e, int m) that you do not want to be serialized during serialization) {name = N; chscore = C; engscore = E; mathscore = m;} public double avgscore () {return (chscore + engscore + mathscore)/3.0 ;} public void printdata () {system. out. println ("name:" + name); system. out. println ("language:" + chscore); system. out. println ("English:" + engscore); system. out. println ("mathematics:" + mathscore); system. out. println ("average:" + avgscore ());}}
Write a class to write serialized data:
Package ch21; import Java. io. *; public class objectserializationexample1 {public static void main (string argv []) {int c = integer. parseint (argv [1]); int e = integer. parseint (argv [2]); int M = integer. parseint (argv [3]); Student ST = new student (argv [0], C, E, m); ST. printdata (); try {fileoutputstream fout = new fileoutputstream ("C: \ data.txt"); objectoutputstream objout = new objectoutputstream (fout); // data stream Connection Machine System !!! Objout. writeobject (ST); // write the serialized object objout. close (); fout. close ();} catch (ioexception IE) {system. err. println (e );}}}
Read serialized data:
Package ch21; import Java. io. *; public class objectserializationexample2 {public static void main (string argv []) {try {fileinputstream fin = new fileinputstream ("data.txt"); objectinputstream objin = new objectinputstream (FIN ); // data stream connection !!! Student ST = (student) objin. readobject (); // get the serialized data St. printdata (); objin. close (); Fin. close ();} catch (exception e) {system. err. println (e );}}}
Result:
Name: Java
Language: 90
English: 80
Mathematics: 0
Average: 56.666666666666664
The transient keyword works.