ObjectInputStream and ObjectOutputStream can write objects on the hard disk or read them from the hard disk, and of course they can be transmitted over the network. But the manipulated object must be serialized.
What is the serialization of an object? The goal of object serialization is to save the object on disk, or to allow the object to be transferred across the network.
The API describes the following
The description in the API is as follows
An object that implements serialization package Cn.bean.demo.io.object;import java.io.serializable;//If this object has an instance property "and the object of this instance is a custom object, He must also be serialized, otherwise the person will not be successfully written to disk and can self-authenticate the "public class person implements serializable{/** * Object serialization version number */private static final Long Serialversionuid = 8761043201815925119l;private string Name;private string sex;private string Age;public string GetN Ame () {return name;} public void SetName (String name) {this.name = name;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;} Public String Getage () {return age;} public void Setage (String age) {this.age = age;} Public person (string name, String sex, String age) {super (); this.name = Name;this.sex = Sex;this.age = age;} @Overridepublic String toString () {return "person [name=" + name + ", sex=" + Sex + ", age=" + Age + "]";}}
Ways to read and write objects package Cn.bean.demo.io.object;import Java.io.fileinputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.ObjectInputStream; Import Java.io.objectoutputstream;import Java.io.outputstream;public class Objectoutandinputstreamtest {public static void Main (string[] args) {//testobjectoutputstream (); Testobjectinputstream ();} /** * Writes an object to a disk file */static void Testobjectoutputstream () {//try (OutputStream out = new FileOutputStream ("Object.txt"); Ob Jectoutputstream ObjectOutputStream = new ObjectOutputStream (out);) The {///write object must implement the serialization of person man = new Person ("Nigulas", "Male", "a"); Objectoutputstream.writeobject (person);} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} /** * Read object information from disk file */static void Testobjectinputstream () {try (InputStream in = new FileInputStream ("Object.txt"); objecti Nputstream ObjectInputStream = new ObjectInputStream (in);) {person person= (person) oBjectinputstream.readobject (); SYSTEM.OUT.PRINTLN (person);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}
Object Flow ObjectInputStream and ObjectOutputStream in Javaio