Connect to a Java fundamentals---I/O technology (ii)
Serialization and deserialization of Java objects What is called serialization and deserialization of objects
To complete an object's input or output, you must also rely on the object output stream (ObjectOutputStream) and the object input stream (ObjectInputStream). The steps to output a serialized object using an object output stream are sometimes serialized, while the process of using an object input stream to read into an object is sometimes called deserialization
After an object is created, it actually opens up a storage space in memory, which facilitates the storage of information,
Object serialization is a method of turning an object into a binary data stream, which can be disproved by object serialization for the transmission or storage of objects.
Note: If an object of a class wants to be serialized, the class in which the object resides must implement the Java.io.Serializable interface
Purpose of Object serialization
- Persistence: The object's byte sequence is persisted to the hard disk, usually stored in a file, such as: the implementation of hibernation, after the server session management, hibernate to persist the object implementation.
- Network traffic: A sequence of bytes that transmits objects on the network, such as data communication between servers, object passing
Seriaversionuid
When an object is serialized or deserialized, consider the JDK version problem, and if the serialized JDK and the deserialized JDK version are not uniform, an exception may occur, so introduce a Seriaversionuid constant in the sequence operation to verify the consistency of the version.
Object serialization and deserialization operations
Object serialization relies on ObjectOutputStream, object deserialization relies on ObjectInputStream
Serialization: ObjectOutputStream
Direct Use Example
ImportJava.io.File;ImportJava.io.FileOutputStream;ImportJava.io.OutputStream;ImportJava.io.ObjectOutputStream; Public classobjectoutputstream{ Public Static voidMain (String args[])throwsException {File f=NewFile ("D:" + file.separator + "test.txt");//Define Save PathObjectOutputStream Oos =NULL;//declaring an object output streamOutputStream out =NewFileOutputStream (f);//file output streamOos =NewObjectOutputStream (out); Oos.writeobject (NewPerson ("Zhang San", 30));//Save ObjectOos.close ();//Close }};
Deserialization: ObjectInputStream
ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.InputStream;ImportJava.io.ObjectInputStream; Public classobjectinputstream{ Public Static voidMain (String args[])throwsException {File f=NewFile ("D:" + file.separator + "test.txt");//Define Save PathObjectInputStream Ois =NULL;//declaring an object input streamInputStream input =NewFileInputStream (f);//file input streamOIS =NewObjectInputStream (input);//instantiating an object input streamObject obj = Ois.readobject ();//Reading ObjectsOis.close ();//CloseSystem.out.println (obj); }};
Transient keywords
In a serialization operation, if a property does not want to be serialized, you can use the Transient keyword to declare
Java I/o---fallback stream
Fallback: gives the user the opportunity to read the second time.
Fallback stream
In Java IO, the data are all in sequential reading, that is, for an input stream is read from start to finish, if the input stream is not necessary to read the content, it can only be used by the program to remove these unwanted content, in order to solve this problem of reading, In Java, a fallback input stream (Pushbackinputstream,pushbackreader) is provided, so that some data overrides that are read in are returned to the buffer of the input stream.
Using InputStream to read continuously using the Read () method is sequential reading.
Fallback flow operation mechanism
Common methods of Pushbackinputstream class
ImportJava.io.ByteArrayInputStream;ImportJava.io.PushbackInputStream; Public classpushinputstreamdemo{ Public Static voidMain (String args[])throwsException {//all exceptions ThrownString str = "www.mldnjava.cn";//Defining StringsPushbackinputstream push =NULL;//defining a fallback stream objectBytearrayinputstream Bai =NULL;//defining the memory input streamBai =NewBytearrayinputstream (Str.getbytes ());//instantiating a memory input streamPush =NewPushbackinputstream (BAI);//reading data from memorySystem.out.print ("The data after reading is:") ; inttemp = 0 ; while((Temp=push.read ())!=-1) {//Read Content if(temp== '. ') {//determine if you have read "."Push.unread (temp);//put it back in the buffer.temp = Push.read ();//read it again .System.out.print ("(Return" + (Char) temp+ ")") ; }Else{System.out.print (Char) temp);//Output Content } } }};
Java Fundamentals---I/O technology (iii)