欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java实现序列化的两种方式,java序列化的方式

来源: javaer 分享于  点击 12876 次 点评:29

java实现序列化的两种方式,java序列化的方式


1.Serializable接口

2.Exteranalizable接口

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(); } }

 

 

相关文章

    暂无相关文章
相关栏目:

用户点评