zoukankan      html  css  js  c++  java
  • Java学习 (序列化)

    序列化
    import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan, Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/employee.ser"); }catch(IOException i) { i.printStackTrace(); } } }

    一个类的对象要想序列化成功,必须满足两个条件:

    该类必须实现 java.io.Serializable 对象。

    该类的所有属性必须是可序列化的。如果有一个属性不是可序列化的,则该属性必须注明是短暂的。

    如果你想知道一个Java标准类是否是可序列化的,请查看该类的文档。检验一个类的实例是否能序列化十分简单, 只需要查看该类有没有实现java.io.Serializable接口。

    反序列化

    import java.io.*;
    
    public class SerializeDemo
    {
       public static void main(String [] args)
       {
          Employee e = new Employee();
          e.name = "Reyan Ali";
          e.address = "Phokka Kuan, Ambehta Peer";
          e.SSN = 11122333;
          e.number = 101;
          try
          {
             FileOutputStream fileOut =
             new FileOutputStream("/tmp/employee.ser");
             ObjectOutputStream out = new ObjectOutputStream(fileOut);
             out.writeObject(e);
             out.close();
             fileOut.close();
             System.out.printf("Serialized data is saved in /tmp/employee.ser");
          }catch(IOException i)
          {
              i.printStackTrace();
          }
       }
    }
  • 相关阅读:
    函数之装饰器
    前端笔记之css
    前端笔记之html
    python之函数
    python之文件操作
    python基础知识
    ovirt一种基于kvm的开源虚拟化软件
    python2与3的区别
    TP框架设置验证码
    js原生子级元素阻止父级元素冒泡事件
  • 原文地址:https://www.cnblogs.com/tampa/p/5829644.html
Copyright © 2011-2022 走看看