生活随笔
收集整理的這篇文章主要介紹了
Java对象序列化
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?? 對于一個存在Java虛擬機中的對象來說,其內部的狀態只是保存在內存中。JVM退出之后,內存資源也就被釋放,Java對象的內部狀態也就丟失了。而在很多情況下,對象內部狀態是需要被持久化的,將運行中的對象狀態保存下來(最直接的方式就是保存到文件系統中),在需要的時候可以還原,即使是在Java虛擬機退出的情況下。
?? 對象序列化機制是Java內建的一種對象持久化方式,可以很容易實現在JVM中的活動對象與字節數組(流)之間進行轉換,使用得Java對象可以被存儲,可以被網絡傳輸,在網絡的一端將對象序列化成字節流,經過網絡傳輸到網絡的另一端,可以從字節流重新還原為Java虛擬機中的運行狀態中的對象。
1.相關的接口
????? Java類中對象的序列化工作是通過ObjectOutputStream和ObjectInputStream來完成的。?????????
???
Java代碼 ?
ObjectOutputStream(OutputStream?out); ??????void?writeObject(Object?obj);????? ??????ObjectInputStream(InputStream?in); ??????Object?readObject();?? ObjectOutputStream(OutputStream out);void writeObject(Object obj);//將指定的對象的非transient,非static屬性,寫入ObjectOutputStreamObjectInputStream(InputStream in);Object readObject();//從指定的流中讀取還原對象信息
??? 只能使用readObject()|writeObject()方法對對象進行讀寫操作。除對象之外,Java中的基本類型和數組也可以被序列化,對于基本類型,可以使用readInt(),writeInt(),
readDouble(),writeDouble()等類似的接口進行讀寫。
2.Serializable接口?
??? 對于任何需要被序列化的對象,都必須要實現接口Serializable,它只是一個標識接口,本身沒有任何成員,只是用來標識說明當前的實現類的對象可以被序列化.
3.transient關鍵字
?
??? 如果在類中的一些屬性,希望在對象序列化過程中不被序列化,使用關鍵字transient標注修飾就可以.當對象被序列化時,標注為transient的成員屬性將會自動跳過。
?
4.Java序列化中需要注意:
????? (1).當一個對象被序列化時,只保存對象的非靜態成員變量,不能保存任何的成員方法,靜態的成員變量和transient標注的成員變量。
??? (2).如果一個對象的成員變量是一個對象,那么這個對象的數據成員也會被保存還原,而且會是遞歸的方式。
??? (3).如果一個可序列化的對象包含對某個不可序列化的對象的引用,那么整個序列化操作將會失敗,并且會拋出一個NotSerializableException。可以將這個引用標記transient,那么對象仍然可以序列化。
5.一個綜合實例:
?
Java代碼 ?
class?Student?implements?Serializable{ ?????? ??????private?String?name; ??????private?transient?int?age; ??????private?Course?course; ?????? ??????public?void?setCourse(Course?course){ ??????????this.course?=?course; ??????} ?????? ??????public?Course?getCourse(){ ??????????return?course; ??????} ?????? ??????public?Student(String?name,?int?age){ ??????????this.name?=?name; ??????????this.age?=?age; ??????} ????????public?String??toString(){ ??????????return?"Student?Object?name:"+this.name+"?age:"+this.age; ??????} ??} ????class?Course?implements?Serializable{ ?????? ??????private?static?String?courseName; ??????private?int?credit; ?????? ??????public?Course(String?courseName,?int?credit){ ??????????this.courseName??=?courseName; ??????????this.credit?=?credit; ??????} ?????? ??????public?String?toString(){ ?????????? ??????????return?"Course?Object?courseName:"+courseName ?????????????????+"?credit:"+credit; ??????} ??}??
class Student implements Serializable{private String name;private transient int age;private Course course;public void setCourse(Course course){this.course = course;}public Course getCourse(){return course;}public Student(String name, int age){this.name = name;this.age = age;}public String toString(){return "Student Object name:"+this.name+" age:"+this.age;}
}class Course implements Serializable{private static String courseName;private int credit;public Course(String courseName, int credit){this.courseName = courseName;this.credit = credit;}public String toString(){return "Course Object courseName:"+courseName+" credit:"+credit;}
}
將對象寫入文件,序列化
?
Java代碼 ?
public?class?TestWriteObject{ ????????public?static?void?main(String[]?args)?{ ????????????String?filePath?=?"C://obj.txt"; ??????????ObjectOutputStream?objOutput?=?null; ??????????Course?c1?=?new?Course("C?language",?3); ??????????Course?c2?=?new?Course("OS",?4); ?????????? ??????????Student?s1?=?new?Student("king",?25); ??????????s1.setCourse(c1); ?????????? ??????????Student?s2?=?new?Student("jason",?23); ??????????s2.setCourse(c2); ????????????try?{ ??????????????objOutput?=?new?ObjectOutputStream(new?FileOutputStream(filePath)); ??????????????objOutput.writeObject(s1); ??????????????objOutput.writeObject(s2); ??????????????objOutput.writeInt(123); ??????????}?catch?(FileNotFoundException?e)?{ ??????????????e.printStackTrace(); ??????????}?catch?(IOException?e)?{ ??????????????e.printStackTrace(); ??????????}finally{ ??????????????try?{ ??????????????????objOutput.close(); ??????????????}?catch?(IOException?e)?{ ??????????????????e.printStackTrace(); ??????????????} ??????????} ?????????? ??????????System.out.println("Info:對象被寫入"+filePath); ??????}??
public class TestWriteObject{public static void main(String[] args) {String filePath = "C://obj.txt";ObjectOutputStream objOutput = null;Course c1 = new Course("C language", 3);Course c2 = new Course("OS", 4);Student s1 = new Student("king", 25);s1.setCourse(c1);Student s2 = new Student("jason", 23);s2.setCourse(c2);try {objOutput = new ObjectOutputStream(new FileOutputStream(filePath));objOutput.writeObject(s1);objOutput.writeObject(s2);objOutput.writeInt(123);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally{try {objOutput.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("Info:對象被寫入"+filePath);}
從文件中讀取對象,反序列化
?
Java代碼 ?
public?class?TestReadObject??{ ?????? ??????public?static?void?main(String[]?args)?{ ?????????? ??????????String?filePath?=?"C://obj.txt"; ??????????ObjectInputStream?objInput?=?null; ??????????Student?s1?=?null,s2?=?null; ??????????int?intVal?=?0; ?????? ??????????try?{ ??????????????objInput?=?new?ObjectInputStream(new?FileInputStream(filePath)); ??????????????s1?=?(Student)objInput.readObject(); ??????????????s2?=?(Student)objInput.readObject(); ??????????????intVal?=?objInput.readInt(); ?????????????? ??????????}?catch?(FileNotFoundException?e)?{ ??????????????e.printStackTrace(); ??????????}?catch?(IOException?e)?{ ??????????????e.printStackTrace(); ??????????}catch(ClassNotFoundException?cnfe){ ??????????????cnfe.printStackTrace(); ??????????}finally{ ?????????????? ??????????????try?{ ??????????????????objInput.close(); ??????????????}?catch?(IOException?e)?{ ??????????????????e.printStackTrace(); ??????????????} ??????????} ?????????? ??????????System.out.println("Info:文件"+filePath+"中讀取對象"); ??????????System.out.println(s1); ??????????System.out.println(s1.getCourse()); ??????????System.out.println(s2); ??????????System.out.println(s2.getCourse()); ??????????System.out.println(intVal); ??????} ??}??
public class TestReadObject {public static void main(String[] args) {String filePath = "C://obj.txt";ObjectInputStream objInput = null;Student s1 = null,s2 = null;int intVal = 0;try {objInput = new ObjectInputStream(new FileInputStream(filePath));s1 = (Student)objInput.readObject();s2 = (Student)objInput.readObject();intVal = objInput.readInt();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}catch(ClassNotFoundException cnfe){cnfe.printStackTrace();}finally{try {objInput.close();} catch (IOException e) {e.printStackTrace();}}System.out.println("Info:文件"+filePath+"中讀取對象");System.out.println(s1);System.out.println(s1.getCourse());System.out.println(s2);System.out.println(s2.getCourse());System.out.println(intVal);}
}
輸出:
[TestWriteObject]
Info:對象被寫入C://obj.txt
[TestReadObjec]
Info:文件C://obj.txt中讀取對象
Info:文件C://obj.txt中讀取對象
Student Object name:king age:0
Course Object courseName:null credit:3
Student Object name:jason age:0
Course Object courseName:null credit:4
123
可知Person中的age屬性被標注為transient后,在序列化對象時,age屬性就沒有被序列化了; Course中的name屬性被static后,Course的name靜態屬性就沒有被序列化;雖然是序列化Person對象,但是Person所引用的Course對象也被初始化了。?
??
http://yuyiming.iteye.com/blog/1277089
轉載于:https://blog.51cto.com/crayworklife/852891
總結
以上是生活随笔為你收集整理的Java对象序列化的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。