Serialization, deserialization, and IO serialization of Java-io stream objects
- Basic serialization operations
1. Object serialization means to convert an Object into a byte sequence, and vice versa.
2. The ObjectOutputStream and writeObject methods are used to write objects to the output stream;
The deserialization stream (ObjectInputStream). The readObject method is used to read objects from the input stream.
3. Serializeable)
The object must implement the serialization interface before it can be serialized. Otherwise, an exception occurs. This interface does not have any method, but is a standard.
Package com. test. io; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. objectInputStream; import java. io. objectOutputStream; public class objectserialetest {/*** Object serialization * @ param file * @ throws Exception */public void ObjectOutput (String file) throws Exception {ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream (file); Student stu = new Student ("002", "Zhang Si", 12); oos. writeObject (stu); oos. flush (); oos. close ();}/*** deserialization of the object * @ param file * @ throws Exception */public void ObjectInput (String file) throws Exception {ObjectInputStream ois = new ObjectInputStream (new FileInputStream (file); Student stu = (Student) ois. readObject (); System. out. println (stu. toString (); ois. close ();} public static void main (String [] args) throws Exception {String file = "F: \ javaio \ obj. dat "; objectserialetest ost = new objectserialetest (); ost. objectOutput (file); ost. objectInput (file );}}