Java basics --- I/O technology (3), basics --- I
Followed by the previous java basics-I/O technology (2)
Java object serialization and deserialization what is object serialization and deserialization?
To complete the input or output of an object, you must also rely on the object output stream (ObjectOutputStream) and object input stream (ObjectInputStream ). The process of outputting serialized objects using an object output stream is sometimes serialized. The process of reading objects using an object input stream is also called deserialization.
After an object is generated, it actually opens up a bucket for it in the memory to facilitate the storage of information,
Object serialization is a method to convert an object into a binary data stream. Object serialization can be used to refute the transfer or storage of objects.
Note: If the object of a class is to be serialized, the class of the object must implement the java. io. Serializable interface.
Purpose of Object serialization
- Persistence: the object's byte sequence is permanently stored on the hard disk and usually stored in a file, such as the implementation of sleep, server session management in the future, and Hibernate implements Object persistence.
- Network Communication: transmits object byte sequences over the network, such as data communication between servers and object transmission.
SeriaVersionUID
JDK versions must be considered when objects are serialized or deserialized. If the serialized JDK and deserialized JDK versions are inconsistent, exceptions may occur, therefore, a seriaVersionUID constant is introduced in the sequence operation to verify version consistency.
Object serialization and deserialization
Object serialization relies on ObjectOutputStream, and object deserialization relies on ObjectInputStream.
Serialization: ObjectOutputStream
Use instance directly
Import java. io. file; import java. io. fileOutputStream; import java. io. outputStream; import java. io. objectOutputStream; public class ObjectOutputStream {public static void main (String args []) throws Exception {File f = new File ("D:" + File. separator + "test.txt"); // defines the storage path ObjectOutputStream oos = null; // declares the object output stream OutputStream out = new FileOutputStream (f ); // file output stream oos = new ObjectOutputStream (out); oos. writeObject (new Person ("Zhang San", 30); // Save the object oos. close (); // close }};
Deserialization: ObjectInputStream
Import java. io. file; import java. io. fileInputStream; import java. io. inputStream; import java. io. objectInputStream; public class ObjectInputStream {public static void main (String args []) throws Exception {File f = new File ("D:" + File. separator + "test.txt"); // defines the storage path ObjectInputStream ois = null; // declares the object input stream InputStream input = new FileInputStream (f ); // file input stream ois = new ObjectInputStream (input); // instantiate Object input stream Object obj = ois. readObject (); // read object ois. close (); // close System. out. println (obj );}};
Transient keyword
If you do not want to serialize an attribute in a serialization operation, you can use the transient keyword to declare it.
Java I/O --- rollback stream
Rollback: The second read opportunity is given to the user.
Stream rollback
In java IO, most data is read sequentially, that is, for an input stream, data is read sequentially from start to end. If an unwanted content in the input stream is read, the unwanted content can only be processed by the program. To solve this reading problem, java provides a one-to-one fallback input stream (PushbackInputStream, PushbackReader ), yes. Some of the data that is read is overwritten and returned to the buffer zone of the input stream.
To use InputStream, you must use the read () method to continuously read data, which is sequential.
Rollback stream operation mechanism
Common Methods of PushbackInputStream class
Import java. io. byteArrayInputStream; import java. io. pushbackInputStream; public class PushInputStreamDemo {public static void main (String args []) throws Exception {// All exceptions throw String str = "www.mldnjava.cn"; // defines the String PushbackInputStream push = null; // define the Back-to-Stream object ByteArrayInputStream bai = null; // define the memory input stream bai = new ByteArrayInputStream (str. getBytes (); // instantiate the memory input stream push = new PushbackInputStream (ba I); // read data from memory System. out. print ("the data after reading is:"); int temp = 0; while (temp = push. read ())! =-1) {// read the content if (temp = '. ') {// judge whether the read is ". "push. unread (temp); // put it back into the buffer. temp = push. read (); // read the System again. out. print ("(returned" + (char) temp + ")");} else {System. out. print (char) temp); // output content }}}};