★ 序列化 將一個對象存放到某種類型的永久存儲器上稱為保持。如果一個對象可以被存放到磁盤或磁帶上,或者可以發送到另外一臺機器并存放到存儲器或磁盤上,那么這個對象就被稱為可保持的。(在java中,序列化、持久化、串行化是一個概念。) java.io.Serializable接口沒有任何方法,它只作為一個“標記者”,用來表明實現了這個接口的類可以考慮串行化。類中沒有實現Serializable的對象不能保存或恢復它們的狀態。 ★ 對象圖 當一個對象被串行化時,只有對象的數據被保存;方法和構造函數不屬于串行化流。如果一個數據變量是一個對象,那么這個對象的數據成員也會被串行化。樹或者對象數據的結構,包括這些子對象,構成了對象圖。 ★ 瞬時 transient 防止對象的屬性被序列化。
演示序列化時創建的值對象
package cn.hncu.io.demo2.serial;import java.io.Serializable;public class MyDate implements Serializable { PRivate static final long serialVersionUID = 1L; private int year; private int month; private int day; public MyDate() { super(); } public MyDate(int year, int month, int day) { super(); this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } @Override public String toString() { return year+"年"+month+"月"+day+"日"; }}package cn.hncu.io.demo2.serial;import java.io.Serializable;//在對一個類進行保存或者讀取(計算機硬盤或者本地文件)的時候該類必須實現可序列化接口----及該類必須序列化!!!public class Person implements Serializable { private static final long serialVersionUID = 1L; private String name; private int age; private MyDate birth; /* * 這里將解決一個問題---如果想將一個靜態變量序列化的情況(我們可以用一個非靜態變量來接收靜態變量的賦值),然后輸出時用靜態變量來進行輸出即可達到我們想要的結果!!! */ private static int count = 0;//這里存在一個知識點---靜態變量是不夠被序列化的!! //private transient int num;//這里有一個知識點---如果某個變量被transient修飾的話,該變量也不能夠被序列化! private int num; public Person() { num = ++count; } public Person(String name, int age, MyDate birth) { this(); this.name = name; this.age = age; this.birth = birth; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return num+", "+name+", "+age+", "+birth; }}進行演示的主函數類
package cn.hncu.io.demo2.serial;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class Serial {//對于讀寫文件而言,只有讀文件時才需要判斷文件是否存在或者路徑是否有問題,寫文件時只需要判斷文件路徑是否正確即可!!!! public static void main(String[] args) { writeDemo(); readDemo(); } public static void readDemo(){ File file = new File("d://a//files//person.txt"); if(!file.exists()){ System.out.println("文件不存在!"); return; } try { ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); while(true){//知識點:這個與對于讀寫int類型的數據來講是不同的地方!!! Person p = null; try { p = (Person) in.readObject(); } catch (Exception e) { System.out.println("文件已經讀取完畢!"); break; } System.out.println(p); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void writeDemo(){ File file = new File("d:/a/files/person.txt"); try { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file)); out.writeObject(new Person("Jack",25,new MyDate(1995,12,1))); out.writeObject(new Person("張三愛李四",26,new MyDate(2010,2,10))); out.writeObject(new Person("Rose喜歡玫瑰",21,new MyDate(2015,7,15))); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}新聞熱點
疑難解答