1. Why serialize?
-- Can persist in memory java objects on disk
-- Transfer objects over the network
-- Transfer via RMI (Remote Method Invocation Remote Procedure Call).
Through serialization, the object can be converted into a platform-independent binary stream, deserialized before reuse, and reconverted into a java object.
(Remote procedure calls are for distributed Java applications, blocking developers' details such as different JVMs and network connections. It seems that objects distributed on different JVMs exist in a unified JVM, which can facilitate communication)
2. How to make Java objects serialized?
In Java, you only need to make the target class implement the Serializable interface, without implementing any methods. The Serializable interface is a tag interface used to indicate that a class can be serialized.
3. How to use serialization and deserialization?
Serialization: Use the writeObject() method of the ObjectOutputStream object output stream to write objects to the output stream.
Deserialization: Use the ObjectInputStream object to write the readObject() method of the stream and cast it to a known target class.
4. Serialization of object references
If a class Person member variable refers to another class (such as class PersonInfo). Right now:
class Person implements Serializable{ String name; PersonInfo info; }
If you want to serialize the Person class, you must meet the following requirements: the PersonInfo class can also be serialized, that is, it also implements the Serializable interface.
class PersonInfo implements Serializable
5. Multiple objects refer to the same child object
PersonInfo info = new PersonInfo(“male”,"china"); Person xiaomi = new Person("Xiao Ming",info); Person dabai = new Person("Baobai",info);
If the above three objects are serialized in sequence, the following two objects originally pointed to the same object above, which means that there is an info object. In order to prevent the three info objects from being serialized during each object serialization, Java sets that if the same java object is serialized multiple times, it is only when the object is serialized into a byte sequence output during the first serialization, and then serialization will only point to the number of the first serialization, and the object will not be serialized again.
6. Parent class serialization
If the parent class implements the Serializable interface, the subclass can be serialized automatically and no longer needs to be displayed to implement the interface.
7. Example of using Serializable to save custom data to local
MainActivity is as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; /** * Demo description: * Access ArrayList<custom data> on SDCard. * * Difference between Parcelable and Serializable: * It is recommended to use Parcelable when transferring data between memory, such as transferring data between activities. * For example:/lfdfhl/article/details/10961459 * Serializable is recommended when saving to local or network transmission. */ public class TestSerializableActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(); testSerializable(); } private void testSerializable() { FileOutputStream fileOutputStream=null; ObjectOutputStream objectOutputStream =null; FileInputStream fileInputStream = null; ObjectInputStream objectInputStream = null; ArrayList<Student> studentsArrayList = new ArrayList<Student>(); Student student = null; for (int i = 1; i < 5; i++) { student = new Student(i, "Xiao Ming" + i); (student); } try { //Save data File file = new File(().toString() + +"Test"+ + ""); if (!().exists()) { ().mkdirs(); } if (!()) { (); } fileOutputStream= new FileOutputStream(()); objectOutputStream= new ObjectOutputStream(fileOutputStream); (studentsArrayList); //Fetch the data fileInputStream = new FileInputStream(()); objectInputStream = new ObjectInputStream(fileInputStream); ArrayList<Student> savedArrayList =(ArrayList<Student>) (); for (int i = 0; i < (); i++) { ("Fetched data:" + (i).toString()); } } catch (Exception e) { // TODO: handle exception }finally{ if (objectOutputStream!=null) { try { (); } catch (IOException e) { (); } } if (fileOutputStream!=null) { try { (); } catch (IOException e) { (); } } if (objectInputStream!=null) { try { (); } catch (IOException e) { (); } } if (fileInputStream!=null) { try { (); } catch (IOException e) { (); } } } } }
Student is as follows:
package ; import ; public class Student implements Serializable { private Integer id; private String name; //Note to define this field public static final long serialVersionUID = 9527L; public Student() { super(); } public Student(Integer id, String name) { super(); = id; = name; } public Integer getId() { return id; } public void setId(Integer id) { = id; } public String getName() { return name; } public void setName(String name) { = name; } @Override public String toString() { return "Student [, name=" + name + "]"; } }
as follows:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>