Sequencetation of Java Learning (1)

zhaozj2021-02-16  90

The so-called "object serialization": I feel that this thing is very profound when I have just contacted it. I have seen it in the future, simply: Use it to store text or numbers as simple storage objects.

Specific application: I am not too much experience due to the current accumulation, so I can't think of a better application scene. For example: the program suddenly encounters short power or other faults during the execution, the program is terrible, which is terrible for some applications. This problem can be solved with object serialization because it can save all the contents of the object to the disk file, so that the object execution status is stored, and it can also be read from the file when needed. This solves the problem of data loss!

If you know more, you can tell me more!

How to achieve: Implement is simple! To implement the serializable interface for a serialized object, the interface does not require implementation, Implements Serializable is just to be serialized, then use an output stream (such as a FileoutPutStream) to construct an ObjectOutputStream (object stream Object, then, using the WriteObject (Object Obj) method of the ObjectOutputStream object, you can write the objects of the object (ie, save its status). If you want to recover, you can use the input stream, say so much to see an example! (Under JDK1.4, pass)

Import java.io. *; import java.util. *;

Public class ObjectFiletest {public static void main (string [] args) {manager boss = new manager ("Carl Cracker", 80000, 1987, 12, 15); boss.setbonus (5000); Employee [] staff = new Employee [ 3]; staff [0] = BOSS; staff [1] = new Employee ("Harry Hacker", 50000, 1989, 10, 15); Staff [2] = New Employee ("Tony Tester", 40000, 1990, 1 , 15); try {/ ** * constructed using an object file output stream output stream * FileOutputStream file output stream * ObjectOutputStream output stream objects * / ObjectOutputStream out = new ObjectOutputStream (new FileOutputStream ( "employee.dat")); out. WriteObject (staff); // writes objects in "Employee.dat" out.close (); // Close flow, please keep in mind / ** * Use the file input stream to construct an object input stream * fileInputStream file input stream * ObjectInputStream Object input stream * / ObjectInputStream IN = New ObjectInputStream (New FileInputStream "Employee.dat")); /// readObject () Reads the object from "Employee.dat", requires type conversion EMPLOYEE [] newstaff = (Employee []) in.readObject (); in.close () For (int i = 0; i

class Employee implements Serializable {public Employee () {} public Employee (String n, double s, int year, int month, int day) {name = n; salary = s; GregorianCalendar calendar = new GregorianCalendar (year, month - 1, day); hireDay = calendar.getTime ();} public String getName () {return name;} public double getSalary () {return salary;} public Date getHireDay () {return hireDay;} public void raiseSalary (double byPercent) { Double raise = salary * bypercent / 100; salary = raise;} public string toString () {return getClass (). getname () "[name =" name ", Salary =" Salary ", HIREDAY = " hireday "] ";} private string name; private double salary; private date hired;} class manager extends Employee {public manager (String n, double s, int year, int MONT H, int day) {Super (n, s, year, month, day); bonus = 0;} public double getsalary () {double basalry = super.getsalary (); returnrate @}} public void setbonus;} public void setbonus b) {bonus = b;} public string toString () {return super.tostring () [bonus = " bonus "] ";

In the process of serialization, it will actually encounter a lot of problems. Due to this problem, I will take a break in your eyes (^_^), I will write it out in future articles! thanks for reading!

转载请注明原文地址:https://www.9cbs.com/read-15231.html

New Post(0)