Java object serialization of study (1)

  The so-called "target sequences": Gangchachu think when this thing is very difficult, since the flu seen its simplicity, the simple word: use it as storage can be as simple text or digital storage targets. 

  Specific applications: As I accumulated experience is not too much, can not think of a better scene to the application.    For example: in the process of implementing procedures suddenly encountered short electrical malfunctions or other termination procedures, object state of the current work also will be lost, for some applications, this is terrible.    Object Serialization with this problem can be solved because it can target the full content of the documents stored in the disk, the state of implementation of this object will be stored, the need also could be from the document as it stood then read out, This will solve the problem of data loss! 

  If you know more about the scope of the application can better tell me! 

  How to achieve: to realize the simple!    For the sequence of the need to be the object of achieving Serializable interface without the need to achieve the method implements Serializable only to mark the targets could be the sequence, and then use the output stream (such as: FileOutputStream) to construct an ObjectOutputStream (Object Data Stream) object, and then use ObjectOutputStream object writeObject (Object obj) method can be parameters for the object obj write (that is, to preserve the state), it is necessary to resume the importation of used-that so much of a examples!    (JDK1.4 through debugging) 

  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 [] = new Employee staff [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 
  ( 
  / ** 
  * Use paper output streams flow structure of a target output 
  * FileOutputStream document output streams 
  * Object output streams ObjectOutputStream 
  * / 
  ObjectOutputStream out = new ObjectOutputStream (new 
  FileOutputStream ( "employee.dat")); 
  Out.writeObject (staff); / / object into "employee.dat" 
  Out.close (); / / Close flow, keep in mind 

  / ** 
  * Use paper input flow structure an object input streams 
  * FileInputStream document input streams 
  * ObjectInputStream object input streams 
  * / 
  ObjectInputStream in new ObjectInputStream = (new 
  FileInputStream ( "employee.dat")); 
  / / / ReadObject () object from "employee.dat" read out the need to type conversion 
  Employee newStaff [] = ([] Employee) in.readObject (); 
  In.close (); 

  For (int i = 0; i <newStaff.length; i + +) 
  System.out.println (newStaff [i]); 
  ) 
  Catch (Exception e) 
  ( 
  E.printStackTrace (); 
  ) 
  ) 

  ) 

  / / / Implements Serializable interface for tagging can be the object of the sequence 

  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, the 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 = byPercent salary * / 100; 
  Salary + = raise; 
  ) 

  Public String toString () 
  ( 
  Return getClass (). GetName () 
  + "[Name =" + name 
  + ", Salary =" + salary 
  + "HireDay =" + hireDay 
  + "]"; 
  ) 

  Private String name; 
  Private double salary; 
  Private Date hireDay; 

  ) 

  Class Manager extends Employee 
  ( 
  Public Manager (String n, double s, int year, int month, int day) 
  ( 
  Super (n, s, year, month, day); 
  Bonus = 0; 
  ) 

  Public double getSalary () 
  ( 
  Super.getSalary double baseSalary = (); 
  Return baseSalary + bonus; 
  ) 

  Public void setBonus (double b) 
  ( 
  Bonus = b; 
  ) 

  Public String toString () 
  ( 
  Return super.toString () 
  + "[Bonus =" + bonus 
  + "]"; 
  ) 

  Private double bonus; 

  ) 

  In the process of sequence will actually face many problems, due to the length of this problem, but also to let your eyes rest (^_^) I will be at a later article written!    Thank you read! 

Bookmark it: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Google
  • DotNetKicks
  • DZone
  • Furl
  • Netvouz

Tags:

Releated Articles


0 Comments to “Java object serialization of study (1)”

No Comments. Send your comment.

Leave a Reply

You must be logged in to post a comment.