日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA SE (14)

發(fā)布時間:2023/12/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA SE (14) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

–> JAVA SE (13)
序列化

又稱為持久化,將其寫入磁盤中。
java提供對象序列化——一個對象可以被表示為一個字節(jié)序列,該字節(jié)序列包括該對象的數(shù)據(jù)、對象中數(shù)據(jù)的類型、對象的類型。將序列化對象寫入文件之后,可以從文件中讀出并對其反序列化——對象的數(shù)據(jù)及類型、對象的類型可以用來在內(nèi)存中新建對象
整個過程是Java虛擬機(JVM)獨立的,即一個平臺上序列化的對象可以在另一個完全不同的平臺上反序列化該對象。
ObjectInputStream和ObjectOutputStream類是高層次的數(shù)據(jù)流,包含反序列化和序列化對象的方法。
一個類的對象要想序列化成功,需滿足兩個條件:

  • 該類必須實現(xiàn)java.io.Serializable接口(序列化接口中沒有方法和屬性,僅用于表示可序列化)。
  • 該類的所有屬性必須是可序列化的。若有屬性不是可序列化的,則該屬性必須注明是短暫(transient)的。
  • 序列化對象

    ObjectOutputStream類用來序列化一個對象。
    當序列化一個對象到文件時,按照java的標準約定是給文件一個.ser擴展名。

    package binaryheap.test;import java.io.File; import java.io.FileOutputStream; import java.io.ObjectOutputStream; import java.io.Serializable;public class BH{public static void main(String[] args) {Student student = new Student();student.address = "shenyang";student.age = 12;student.name = "TxL";student.number = 423;student.ssn = 123456;try {File file = new File("D:/serializable.ser");FileOutputStream fos = new FileOutputStream(file);ObjectOutputStream ooStream = new ObjectOutputStream(fos);ooStream.writeObject(student);ooStream.close();fos.close();} catch (Exception e) {e.printStackTrace();} } }class Student implements Serializable{public String name;public String address;public int number;public int age;public transient int ssn;public void mail() {System.out.println("Mailing to " + name + " " + address);} }

    將對象序列化到了一個.ser文件中。

    反序列化對象:

    package binaryheap.test;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; import java.io.Serializable;public class BH{public static void main(String[] args) throws IOException, ClassNotFoundException {Student s = null;try {FileInputStream fin = new FileInputStream("D:/serializable.ser");ObjectInputStream ois = new ObjectInputStream(fin);s = (Student)ois.readObject();ois.close();fin.close();} catch (FileNotFoundException e) {e.printStackTrace();}System.out.println("s.address: " + s.address);System.out.println("s.age: " + s.age);System.out.println("s.name: " + s.name);System.out.println("s.number: " + s.number);System.out.println("s.ssn: " + s.ssn);} }class Student implements Serializable{public String name;public String address;public int number;public int age;public transient int ssn;public void mail() {System.out.println("Mailing to " + name + " " + address);} }

    結(jié)果

    s.address: shenyang s.age: 12 s.name: TxL s.number: 423 s.ssn: 0

    如果JVM在反序列化對象的過程中找不到該類,則拋出ClassNotFooundException異常。
    注意:readObject返回的對象為Object類型,需要強制轉(zhuǎn)換。transient修飾的值沒有被發(fā)送到輸出流。所以,反序列化后,為0。

    總結(jié)

    以上是生活随笔為你收集整理的JAVA SE (14)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。