ObjectOutputStream and ObjectInputStream
- ObjectOutputStream
ObjectInputStream class recovers basic type data and objects after using ObjectOutputStream class.
ObjectOutputStream and ObjectInputStream use FileOutputStream and FileInputStream to support the stable storage of object images to be implemented.
ObjectInputStream can be used to recover previously serialized objects. In addition, this class is also used, such as using a socket to deliver an object between the host,
Or when the object is transmitted in the remote communication system for communication with the parameters and the parameter amount.
ObjectInputStream ensures that the type of all objects in the image created from the stream matches class that appears in the Java virtual machine. Use the standard mechanism to load the corresponding class on demand.
Only objects that support java.io.serializable or java.io.externalizable interfaces can be read from the stream. Use the ReadObject method from the stream
Read an object. Java's security styling should be used to get the desired type. In Java, string and arrays are objects and can be used as an object in the serialization process.
When reading, they need to be converted to the desired type.
In addition, the base type can also be read from the stream using the correct way in DataInput.
The default reverse serialization mechanism of the object restores the contents of each domain to its value and type when it is written. During the reverse serialization process, it is ignored to the temporary or static domain.
Quote for other objects prompted those objects to be read from the stream. Use the reference sharing mechanism to correctly restore the image of the object. Introductive, always assign new objects,
Prevent rewriting existing objects.
Reading an object is similar to the constructor running a new object. The memory allocated for this object is initialized into empty (NULL). No sequentialized classes call no arctive,
Then recover the sequence-class domain from the stream, and restore the sequence object from the closest java.Lang.Object to the end of the specified object.
For example, reading the stream in the ObjectOutputStream in the example:
FileInputStream istream = new FileInputStream ( "t.tmp"); ObjectInputStream p = new ObjectInputStream (istream); int i = p.readInt (); String today = (String) p.readObject (); Date date = (Date) p .readObject (); iStream.close (); Class Controlling their serialization by implementing java.io.serializable or java.io.externalizable interface.
Implementing a serialization interface allows objects to save and restore its full state, allowing classes to improve in writing streams and from the flow read.
It automatically traverses the reference, saves and restores the full image between the objects. A sequentially sequentialized class that requires a particular handle during serialization and in reverse serialization processing.
Must achieve the following two methods:
private void writeObject (java.io.ObjectOutputStream stream) throws IOException; private void readObject (java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; writeObjectmethod method using the state of the object of a special class of a write stream corresponding readObject Methods will be responsible for reading and restoring these data.
This method does not have to care about whether it is a parent class that belongs to it or a subclass. Read the data from ObjectInputStream to restore the status of a single field and assign it to the appropriate domain of the object.
Use the DataInput method to read the basic data type. Serialization Operation The domain value that does not implement the Java.io.Serializable interface, does not read or assigns its domain value. Subclasses of non-serialized objects can be serialized.
In this case, the non-sequentialization class must have a unfolded artifact, so that it can be used to perform initialization using this structure. In this situation,
Subcords are responsible for saving and restoring the state of non-sequentialization. Normally, the field of the parent class is a stored (public, package or protected),
Or there is a usable acquisition or setting method for restoring its state.
ObjectInputStream can get any anomalies that appear during reverse serialization, once an exception occurs, give up the read process.
Implementing an external interface allows objects to fully control the contents and formats of this object serialization.
Call the external interface: WriteExternal and readExternal save and restore the object status. When a class implements these methods,
They can write or read their own status using all methods of the ObjectOutput and ObjectInput methods. The object is responsible for managing the corresponding version it appears.
ObjectOutputStream
Public Class ObjectOutputStream ExtendOutput, ObjectStreamConstants Class ObjectOutputStream writes the basic data types and elements in the Java object to an OutputStream object. These objects can be read using ObjectInputStream.
In addition, files corresponding to this stream can store these objects. If the stream is a network communication stream, these objects can be rebuilt on another host or another processor.
Only objects that support the java.io.serializable interface can be written to the stream. Coding each sequentially, including the name and tag of the corresponding class,
Objects' properties and array values, and any other objects referenced when initializing objects.
Write an object to the stream using WriteObject. Any object, including strings and arrays, both WriteObject methods are written.
Multiple objects or base type objects can also be written to this stream. Conversely, the same type and the same order must be written in these objects,
Read these objects from the corresponding ObjectInputStream stream.
The base type can also be written to this stream using the correct way in Dataoutput. String objects can also be written in WRITEUTF methods.
An object's default serialization mechanism will write to the object's class, class tag, and all non-transitory and non-static properties values.
The reference to other objects (divided or static attributes) will also cause the above objects to be written. Use the sharing mechanism to encode multiple references for a single object,
The primitive of the object can be stored as the same shape as it is written.
For example, write an object, this object can read from ObjectInputStream:
FileOutputStream ostream = new FileOutputStream ( "t.tmp"); ObjectOutputStream p = new ObjectOutputStream (ostream); p.writeInt (12345); p.writeObject ( "Today"); p.writeObject (new Date ()); p. Flush (); ostream.close (); the class that requires a particular handle during the serialization process must use the following appropriate tags to implement a specific method:
private void readObject (java.io.ObjectInputStream stream) throws IOException, ClassNotFoundException; private void writeObject (java.io.ObjectOutputStream stream) throws IOException writeObject method is responsible for writing the state of an object of a particular class, so that the corresponding readObject method can store it. This method does not have to care about the status of the parent class or subclass of the object. Dataoutput supported using WriteObject methods or basic types
Method Saves the state of each domain to ObjectOutputStream.
The serialization operation cannot output a domain that does not implement any object of the Java.io.Serializable interface. Subclasses of non-serialized objects can be serialized.
In this case, the non-sequentialized class must have a unless-array, so that it can be initialized. In this case, the subclass is responsible for saving and restoring the state of the insequence class.
Normally, the domain of the parent class is a stored (public, package or protected), or there is a usable acquisition or setting method for restoring its state.
Implementing the WriteObject and ReadObject methods that throws NotserializableException exceptions can prevent serialization of an object.
ObjectOutputStream will get this exception and give up this serialization process. Implementing an external interface allows objects to fully control the contents and formats of this object serialization.
Call the external interface: WriteExternal and readExternal save and restore the object status. When a class implements these methods,
They can write or read their own status using all methods of the ObjectOutput and ObjectInput methods. The object is responsible for managing the corresponding version it appears.
Import java.io. *; import java.util. *;
Public Class Logon imports serializable {
Private date Date = new date (); private string username; private transient string password;
Logon (String Name, String PWD) {username = name; password = pwd;}
PUBLIC STRING TOSTRING () {string PWD = (Password == NULL)? "(N / A)": password; return "logon info: / n" "Username:" username "/ n Date: Date "/ N Password:" PWD;
public static void main (String [] args) throws IOException, ClassNotFoundException {Logon a = new Logon ( "Morgan", "morgan83"); System.out.println ( "logon a =" a); ObjectOutputStream o = new ObjectOutputStream ("Logon.out"); O.WriteObject (a); o.close (); int seconds = 5; long t = system.currenttimemillis () seconds * 1000; while (system.currenttimemillis () ObjectInputStream in = new ObjectInputStream (new FileInputStream ( "Logon.out")); System.out.println ( "Recovering object at" new Date ()); a = (Logon) in.readObject (); System.out. Println ("Logon a =" a);}} Class Logon is a class that logs in login information, including username and password. First it implements the interface Serializable, which marks it to be serialized. After the main method, ObjectOutputStream O = New ObjectOutputStream (New FileoutputStream ("Logon.out"); Create a new object output stream package a file stream, indicating that the destination of the object serialization is the file logon.out. Then start writing with the method WriteObject. It is also very simple when you want to restore ObjectInputStream in = New fileInputStream (New FileInputStream ("Logon.out"); Create a new object input to file stream logon.out as a parameter, then call ReadObject methods.