java 對象系列化 (Serializable)
對象序列化:就是將一個對象轉換爲二進制的數據流。
如果一個類的對象要想實現對象系列化,則對象所在的類必須實現Serializable接口。在此接口中沒有任何的方法,此接口只是作爲一個標識,表示本來的對象具備了序列化的能力而已。
如果想要完成對象的序列化,則還要依靠ObjectOutputStream類和ObjectInputStream類,前者屬於序列化操作,而後者屬於反序列化操作。
實例:
- import java.io.Serializable;
- public class Person implements Serializable {
- private String name;
- private int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String toString() {
- return "姓名:" + this.name + " 年齡: " + this.age;
- }
- }
使用ObjectOutputStream完成序列化的操作
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.ObjectOutputStream;
- public class ObjectOutputStreamDemo {
- public static void main(String[] args) throws Exception {
- File file = new File("D:" + File.separator + "Person.txt");
- ObjectOutputStream objectOutputStream = null;
- objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
- objectOutputStream.writeObject(new Person("singsong", 23));
- objectOutputStream.close();
- }
- }
使用ObjectInputStream完成反序列化操作
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.ObjectInputStream;
- public class ObjectInputStreamDemo {
- public static void main(String[] args) throws Exception {
- File file = new File("D:" + File.separator + "person.txt");
- ObjectInputStream objectInputStream = null;
- objectInputStream = new ObjectInputStream(new FileInputStream(file));
- Object object = objectInputStream.readObject();
- Person person = (Person) object;
- System.out.println(person);
- }
- }
運行結果:
- 姓名:singsong 年齡: 23
以上的操作實際上是將整個的對象進行了序列化的操作,如果假如類中某個屬性不希望被序列化的話,則要使用transient關鍵字聲明。
實例:修改Person類:
- import java.io.Serializable;
- public class Person implements Serializable {
- private transient String name;
- private transient int age;
- public Person(String name, int age) {
- super();
- this.name = name;
- this.age = age;
- }
- public String toString() {
- return "姓名:" + this.name + " 年齡: " + this.age;
- }
- }
然後再進行序列化和反系列化操作,運行結果:
- 姓名:null 年齡: 0
顯示的是默認值
既然可以對一個對象進行系列化,那麼能不能同時對多個對象一起進行系列化操作
Object類可以接收任意的引用數據類型,包括數組。
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- public class ArraySerDemo {
- public static void main(String[] args) throws Exception {
- Person person[] = { new Person("張三", 21), new Person("李四", 22),
- new Person("王五", 23) };
- ser(person);
- Person[] persons = (Person[]) dser();
- print(person);
- }
- public static void ser(Object object) throws Exception {
- File file = new File("D:" + File.separator + "Person.txt");
- ObjectOutputStream objectOutputStream = null;
- objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
- objectOutputStream.writeObject(object);
- objectOutputStream.close();
- }
- public static Object dser() throws Exception {
- Object temp = null;
- File file = new File("D:" + File.separator + "person.txt");
- ObjectInputStream objectInputStream = null;
- objectInputStream = new ObjectInputStream(new FileInputStream(file));
- temp = objectInputStream.readObject();
- return temp;
- }
- public static void print(Person persons[]) {
- for (Person person : persons)
- System.out.println(person);
- }
- }
運行結果:
- 姓名:張三 年齡: 21
- 姓名:李四 年齡: 22
- 姓名:王五 年齡: 23