帅气的毛毛侠

导航

对象序列化

将那对象序列化到内存中,再将内存中序列化对象反序列化

package ckw.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Main implements Serializable{
    private static final long serialVersionUID = 1L;
    public String name="jjfjf";
    
    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException {
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        Main testObject = new Main();
        oos.writeObject(testObject);
        oos.flush();
        oos.close();
        String serString=fos.toString();
        System.out.println(serString);
        
        ByteArrayInputStream bi=new ByteArrayInputStream(fos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bi);
        Main main = (Main) ois.readObject();
        System.out.println(main.name);
        
    }
    
}

 

posted on 2018-03-08 23:09  帅气的毛毛侠  阅读(113)  评论(0编辑  收藏  举报