zoukankan      html  css  js  c++  java
  • java实现序列化的两种方式

    1.Serializable接口

    2.Externalizable接口

    public class Demo2 implements Externalizable{
    
        transient private String s="sss";
        public int b=10;
        public String ss="iiii";
        public int a=1;
        
        @Override
        public void writeExternal(ObjectOutput out) throws IOException {
            out.writeObject(ss);
            out.writeObject(a);
        }
        @Override
        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
            ss =(String)in.readObject();
            a =(Integer)in.readObject();
        }
        @Override
        public String toString() {
            return "Demo2 [ss=" + ss + ", a=" + a + "]";
        }
    }
    测试:

    public
    class Test { public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException { Demo2 demo2=new Demo2(); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("D:\Desktop\a.txt")); oos.writeObject(demo2); oos.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("D:\Desktop\a.txt")); Object object = ois.readObject(); System.out.println(object); ois.close(); } }

  • 相关阅读:
    2017年6月笔记
    2017年5月笔记
    2017年4月笔记
    转发:i p _ f o r w a r d函数
    IP分组
    IP协议首部结构介绍
    IP:网际协议
    sed工具使用
    正则表达式匹配
    TCP的半连接
  • 原文地址:https://www.cnblogs.com/crazy-lc/p/11862798.html
Copyright © 2011-2022 走看看