Java对对象的序列化和反序列化

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

Java提供了将对象以字节的形式写入到磁盘中,在需要的时候在从磁盘中恢复的功能,在网络传输中非常有用,下面将代码贴上,分别用两种形式实现了对象的序列化,其中第二种当时能将指定的变量序列化,更加人性化的随心所欲

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
 * @use 实现对对象的序列化和反序列化
 * @author Bird
 *
 */
public class Data {
    public static void writeObject() throws Exception{
        Worm worm = new Worm("Bird");
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("d://worm.out"));
        out.writeObject(worm);
        out.close();//关闭的同时也刷新清空了缓冲区
    }
     
     
    public static void readObject() throws Exception{
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://worm.out"));
        Worm s2 = (Worm) in.readObject();
        System.out.println(s2);
    }
     
    public static void main(String[] args) throws Exception{
    //  writeObject();
        readObject();
    }
}
 
 
class Worm implements Serializable{
     
    private static final long serialVersionUID = 1L;
    private String name = null;
    public Worm(String name){
        this.name = name;
    }
     
    public String toString(){
        return name;
    }
}
 

下面这是第二种方式
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
/**
 * @use 第二种可控制序列化变量个数的方式
 * @author Bird
 *
 */
public class Blip3 implements Externalizable {
    private int i;
    private String s;//没有实例化
    public Blip3(){
        System.out.println("Blip3 Constructor!!");
    }
    //注意  这里的s没有实例化
     
    public Blip3(String x, int a){
        System.out.println("Blip3(String x, int a)");
        s = x;
        i = a;
        //s  和 i实例化在非默认构造函数中
    }
     
    public String toString(){
        return s + i;
    }
     
    public void writeExternal(ObjectOutput out){//可选择写入变量
        System.out.println("Blip3.writeExternal");
        try {
            out.writeObject(s);
            out.writeInt(i);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }//必须进行这两项,否则就一个变量也不出初始化
    }
     
    public void readExternal(ObjectInput in){//可选择读入数据
        System.out.println("Blip3.readExternal");
        try {
            s = (String)in.readObject();
            i = in.readInt();
        } catch (ClassNotFoundException e) {
             
        } catch (IOException e) {
        throw new RuntimeException(e);
        }
         
    }
     
    public void read() throws FileNotFoundException, IOException, ClassNotFoundException{//读取序列化的类
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("d://Blip3.out"));
        System.out.println("Revovering  b3");
        Blip3 b3 = (Blip3)in.readObject();
        System.out.println(b3);
    }
     
    public void write() throws Exception{//写入对象
        Blip3 b3 = new Blip3("A String", 47);
        System.out.println(b3);
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("d://Blip3.out"));
        System.out.println("Saving Object");
        o.writeObject(b3);
        o.close();
    }
     
    public static void main(String[] args) throws Exception{
        Blip3 b = new Blip3();
        //  b.write();
        b.read();
    }
}