Java 对象序列化

一、序列化:内存中的 Java 对象------->二进制流

二、目的:

        a) 把对象存储到外部存储器中持久化保存

        b) 通过网络传输对象。

三、可序列化的对象Java 要求可序列化的类实现下面两个接口之一。

         —Serializable :接口只是一个标记性的接口,实现该接口无需实现任何方法;
         —Externalizable : 实现该接口需要实现方法。

四、序列化的 IO 流:

         ObjectInputStream — 负责从二进制流 恢复 对象 --> 从文件中提取对象;
         ObjectOutputStream — 负责将内存中的对象写入磁盘
  示例1:
public class Apple implements Serializable {
     private static final long serialVersionUID = 1L;
     private String name;
     private String color;
     private int weight;

     public Apple() {
       super();
     }
     public Apple(String name, String color, int weight){
       super();
       this.name = name;
       this.color = color;
       this.weight = weight; 
     }
     public String getName() {
        return name; 
     }
     public void setName(String name) {
        this.name = name; 
     }
     public String getColor() {
        return color; 
     }
     public void setColor(String color) {
        this.color = color; 
     }
     public int getWeight() {
        return weight; 
     }
     public void setWeight(int weight) {
        this.weight = weight; 
     }
     @Override
     public String toString() {
        return "Apple [name=" + name + ", color=" + color
               + ", weight="+ weight + "]"; 
     } 
}

测试:

public class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
       Apple apple=new Apple("紅富士","紅色",25); 
       ObjectOutputStream oos = new
       ObjectOutputStream(new
       FileOutputStream("f:/1.txt"));
       oos.writeObject(apple);
 
       ObjectInputStream ois = new
       ObjectInputStream(new FileInputStream("f:/1.txt"));
       System.out.println(ois.readObject());
       oos.close();
       ois.close();
  } 
}

五、引用变量的序列化机制:

        A、引用变量所引用的对象的所有属性都应该是可序列化的。
        B、如果要序列化的对象是之前已经序列化的,此时系统序列化一个编号。这种序列化机制,
              就是为了保存磁盘里的二进制流与内存中的对象是对应的。
【注意】
       A、  transient: 用于修饰实例成员变量 ( 不能与 static 修饰符同时使用 ) 。 用于指定被修饰的
field 不会被序列化。好处:比如银行卡账号、密码就不应该被序列化出来。
      B、由于 static 修饰的类变量存储在类信息中,并不存储在对象里,所以有 static 修饰的类变
量不能被序列化。
示例:自定义序列化类
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    private String account;
    private String password;

    public User() {}
    public User(String account, String password) {
        this.account = account;
        this.password = password; 
    }
    public String getAccount() {
        return account; 
    }
    public void setAccount(String account) {
        this.account = account; 
    }
    public String getPassword() {
        return password; 
    }
    public void setPassword(String password) {
        this.password = password; 
    }
    @Override
    public String toString() {
        return "User [account=" + account + ", password="+ password + "]"; 
    }
   
    // 下面两个方法,提供给系统调用,系统会调用者两个方法完成实际的序列化
    private void writeObject(ObjectOutputStream out) throws IOException {
        // 序列化User的两个属性
        out.writeUTF(account);
        out.writeUTF(new
        StringBuilder(password).reverse().toString());
    }
    private void readObject(ObjectInputStream in)throws 
 IOException,ClassNotFoundException {
        account = in.readUTF();
        password = new
        StringBuilder(in.readUTF()).reverse().toString();
    } 
}

测试:

public class Test {
    public static void main(String[] args) {
        User user = new User("张三", "123");
        //将对象写入到文件中
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new
            FileOutputStream("f:/1.txt"));
            oos.writeObject(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
                    try {
                        oos.close();
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    } 
        }
        //从文件中读取对象
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new
            FileInputStream("f:/1.txt"));
            User u = (User) ois.readObject();
            System.out.println(u.toString());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                ois.close();
            } catch (Exception e2) {
                e2.printStackTrace();
            } 
        } 
     } 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

上庸者-不服周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值