An object stream that directly writes or reads obj.
Preface:
such as drawing the program, BAM draw a triangle out, bam draw a square out. Then save the next time you open the software, triangles, squares are still in the original position. If you use object-oriented thinking, triangles, squares have their own member variables, x, y coordinates, color, line color, thickness, etc., when you save this content to write to the hard disk, write to the file, which is called disk. For example, a block, you save as long as the starting point coordinates, width, height color and so on, when the display again in the same place to draw it.
Serialization: Converts an object directly into a byte stream to write to the hard disk or write to the network.
Actually in memory will be new out of the block of an object, has its various properties, save the time to put these attributes are stored in the file, with DataOutputStream can be achieved, regardless of its properties is Int,dubbo and so on. Since the contents of this object must be written to the hard disk, why not write the whole object into it, because the object new out of the time is nothing more than an area of memory, that is, a byte of a byte, simply the whole byte, this small piece of memory all written into the hard disk, more appropriate, More convenient, this is the original intention of object. Of course, new comes out of the object in addition to their own properties, there is a version of the identity object, this, super pointer, the relevant content will be written in. This is the function of the object stream. This is serialization.
java.io.Serializable:
Serializable. If you want to write an object of a class to a hard disk or a network, and you want to serialize it into a byte stream, you must implement the serialization interface.
All the subtypes of a serializable class are themselves serializable.
There is no method, a labeled interface, for the compiler to see, and the compiler sees this interface and knows that the class can be serialized.
Example program:
PackageLhy.io;ImportJava.io.*; Public classTestobjectio { Public Static voidMain (String args[])throwsException {T T=NewT (); T.K= 8; //write objects,. dat files for saving data//file output streamFileOutputStream fos =NewFileOutputStream ("D:/share/testobjectio.dat"); //set of one-layer object output streamsObjectOutputStream Oos =NewObjectOutputStream (FOS); //Write ObjectOos.writeobject (t); Oos.flush (); Oos.close (); //ReadFileInputStream FIS =NewFileInputStream ("D:/share/testobjectio.dat"); ObjectInputStream Ois=NewObjectInputStream (FIS); //read an objectT treaded =(T) ois.readobject (); //10 9 2.3 0 Small babes, transient the member variable K, which cannot be serialized, is the int default value of 0System.out.println (treaded.i + "+ TREADED.J +" "+ Treaded.d +" "+ treaded.k+" +treaded.str); }}/*** Implementation of the serialization interface*/classTImplementsserializable{inti = 10; intj = 9; DoubleD = 2.3; //transient: Transparent, not considered when serializing transient intK = 15; String Str= "Xiao Yang zi";}
Java object Flow and serialization