第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定

談?wù)?java 中的序列化

標(biāo)簽:
Java

Java 序列化机制为了节省磁盘空间,具有特定的存储规则,当写入文件的为同一对象时,并不会再将对象的内容进行存储,而只是再次存储一份引用,上面增加的 5 字节的存储空间就是新增引用和一些控制信息的空间。反序列化时,恢复引用关系, t1 和 t2 指向唯一的对象,二者相等,输出 true。该存储规则极大的节省了存储空间。

public class TestSerialTwo {
  public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
     ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("res.obj"));
     TestSerial test = new TestSerial();
     // 试图两次将对象写入文件
     out.writeObject(test);
     out.flush();
     System.out.println(new File("res.obj").length());
     out.writeObject(test);
     out.close();
     System.out.println(new File("res.obj").length());
     ObjectInputStream oin = new ObjectInputStream(new FileInputStream("res.obj"));

     // 从文件中依次读出两个对象
     TestSerial t1 = (TestSerial) oin.readObject();
     TestSerial t2 = (TestSerial) oin.readObject();
     oin.close();
     // 判断两个引用是否指向同一个对象
     System.out.println(t1 == t2);

  }
}

静态变量序列化,序列化并不保存静态变量

public class TestStaticSerial implements Serializable {
  private static final long serialVersionUID = 1L;
  public static int staticVar = 5;

  public static void main(String[] args) {
     try {
       // 初始化时staticVar为5
       ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("result.obj"));
       out.writeObject(new TestStaticSerial());
       out.close();
       TestStaticSerial.staticVar = 10;
       // 序列化后改为10
       ObjectInputStream oin = new ObjectInputStream(new FileInputStream("result.obj"));
       TestStaticSerial t = (TestStaticSerial) oin.readObject();
       oin.close();

       // 再读取一次staticVar的新值;
       System.out.println(t.staticVar);

     } catch (Exception e) {
       // TODO: handle exception
     }
  }
}
點(diǎn)擊查看更多內(nèi)容
1人點(diǎn)贊

若覺得本文不錯(cuò),就分享一下吧!

評論

作者其他優(yōu)質(zhì)文章

正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消