Serialization is a problem that we often encounter during the development process. You will often see a class that implements the serializable interface. This is the implementation of serialization.
I'm going to talk about serialization in a small series today.
Java is object-oriented programming, where objects hold the complete data of a class. But Java has its own garbage collection mechanism, and when an object is no longer being used, it is reclaimed and freed up. What if we want to preserve the data of an object for a long time? This is the time to use serialization.
Serialization is the transformation of the state information of an object into a process that can be stored or transmitted. During serialization, an object writes its current state to a temporary or persistent store. You can override the creation of the object later by reading it from the store or deserializing its state. Serialization can not only persist an object, but also facilitate the sharing of object data among different programs, while the program is more convenient to maintain.
How is serialization implemented?
import java.io.serializable; public class person implements serializable{ private static final long serialversionuid = 1L; public String name; public int age; public double money; private static final long serialversionuid = 1l;public person (String name, int age, double money) {this.name = name;this.age = Age;this.money = money;} @Overridepublic string tostring () {return "Name is:" +name+ " , age is:" +age+ " , money is:" +money;} }
Here we find a class that implements an interface but does not have the means to implement it. This is because the class implements the serializable interface by implementing the serialization feature. The serialized interface has no methods and fields and is used only to identify serializable. It should be emphasized here that serializable subclasses are serializable. Serialization is the data that holds the object, and if the data to get the object is deserialized.
Serialization and deserialization are implemented through the WRITEOBEJCT () and the ReadObject () two methods.
The WriteObject () method is responsible for writing the object state of the feature class. By calling Out.defaultwriteobject, you can invoke the default mechanism of the field that holds the Object, which itself does not need to involve states that belong to its superclass or subclass.
The ReadObject () method is responsible for reading and recovering class fields from the stream. It can call In.defaultreadobject to invoke the default mechanism to restore non-static and non-transient fields of the object.
A Java file serializes an object
import java.io.FileNotFoundException; import java.io.fileoutputstream; import java.io.ioexception; import java.io.objectoutputstream; public class Demotext01 { public static void main (String [] args) { Person person = new Person ("Joinname", 20, 100.89) //serialized Object --> Only serializable classes, objects can be serialized try { objectoutputstream oos = new objectoutputstream (New fileoutputstream ("D:/save.txt")); Oos.writeobject (person); system.out.println ("Save Object"); oos.close () ; } catch (filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } } }
Another Java file deserializes an object
import java.io.FileInputStream; import java.io.filenotfoundexception; import java.io.ioexception; import java.io.objectinputstream; public class Demotext02 { public static void main (String [] args) { try { Objectinputstream ois = new objectinputstream (New fileinputstream ("D:/save.txt")); Person person = (person ) ois.readobject (); SYSTEM.OUT.PRINTLN (person); ois.close () ; } catch (Filenotfoundexception e) { e.printstacktrace (); } catch (ioexception e) { e.printstacktrace (); } catch (classnotfoundexception e) { // todo auto-generated catch block e.printstacktrace (); } } }
The save file type that needs to be emphasized for production can be any type.
This article is from the "Weare Program ape" blog, please be sure to keep this source http://liyijieapp.blog.51cto.com/9221814/1575803
Program Ape talking about the serialization of Java