First lecture object serialization (persisted)
First, the summary: is to hold the object in the hard disk, can keep the data; key classes: ObjectInputStream and Objectoutpurstream
Two
Keyword: ObjectOutputStream: Method has Writerobject () Read ObjectInputStream method has ReadObject () The object being serialized requires Implements Serializable
The class to be serialized needs to be implemented serializable
It is equal to a pinch, which is used for identification, and changes the statements in the class. If you want to fix a pinch, you can:
public static final long serialversionuid=42l;//he fixed a pinch
1. Write to stream object
Use the Writerobject (object obj) method to pass an object as a parameter
2. Read Stream object
Adaptive readobject (Object obj) method, method
Note: cannot be serialized and saved after being modified by static and transient
1 /*The main purpose of this is to demonstrate how object serialization2 * Keyword: objectoutputstream: Method has Writerobject () Read3 * ObjectInputStream method has readobject ()4 * The serialized object needs to be implements Serializable5 * 6 */7 ImportJava.io.*;8 Public classObjectstreamdemo {9 Public Static voidMain (string[] args)throwsioexception,classnotfoundexception{Ten OnePerson p=NewPerson ("water tone", "KR"); A -File f=NewFile ("D:\\sy.txt"); - the writeobj (p,f); - readobj (f); - } - + Private Static voidwriteobj (person P, File f) { -ObjectOutputStream oos=NULL; + Try { Aoos=NewObjectOutputStream (NewFileOutputStream (f)); at Oos.writeobject (p); -}Catch(IOException e) { - Throw NewRuntimeException ("Object Write Failed"); - } - finally{ - Try { in if(oos!=NULL) - oos.close (); to}Catch(Exception E2) { + Throw NewRuntimeException ("Closed stream failed"); - } the } * } $ //ReadPanax Notoginseng Private Static voidReadobj (File f) { -ObjectInputStream ois=NULL; the Try { +ois=NewObjectInputStream (NewFileInputStream (f)); APerson p=(person) ois.readobject (); the System.out.println (p); +}Catch(Exception e) { - //Todo:handle Exception $ } $ finally{ - Try { - if(ois!=NULL) the ois.close (); -}Catch(Exception e3) {Wuyi Throw NewRuntimeException ("Closed stream failed"); the } - } Wu - } About //Video Read $ Public Static voidReadobj ()throwsIOException, classnotfoundexception{ -ObjectInputStream ois= - NewObjectInputStream (NewFileInputStream ("D:\\sy.txt")); -Person p=(person) ois.readobject (); A System.out.println (p); + ois.close (); the } - $ //Video Method Storage the Public Static voidWriteobj ()throwsioexception{ the theObjectOutputStream oos= the NewObjectOutputStream (NewFileOutputStream ("D:\\sy.txt")); -Oos.writeobject (NewPerson ("water tone", "KR")); inOos.writeobject (NewPerson ("water tone", 25)); the oos.close (); the } About } the the //Create Class the classPersonImplementsserializable{//this must be implemented to be serialized + Public Static Final Longserialversionuid=42l;//fix him a pinch . - the PrivateString name;Bayi transient intAge//cannot be serialized after use of transient the StaticString country= "cn";//Static also cannot be serialized thePerson (String name,intage,string Country) { - This. name=name; - This. age=Age ; the This. country=Country; the } the PublicString toString () { the returnname+ "=" +age+ "=" +Country; - } the}
Dark Horse programmer--java Basic--io Stream (iii)-sequence flow, pipeline flow, Randomaccessfile class, stream object manipulating basic data type, operation array and string, character encoding