【BigData】Java基础_ObjectOutputStream与ObjectInputStream实现序列化与反序列化

1.概念

ObjectOutputStream用于序列化

ObjectOutputStream用于反序列化

所谓的序列化,其实就是将对象转化为二进制

举个例子说明:

例如,我在京东上买了一张木床,京东发货的时候,肯定不会给我发一张已经拼接好的木床,因为这样不方便运输,那么快递公司会将床先拆开,然后附上安装说明书,这就叫做序列化,等我拿到床的时候,我再把床按照说明书组装起来,这就叫做反序列化

2.代码案例

2.1 简单对象的序列化与反序列化

文件1:User.java 一个存储用户信息的java类

package cn.test.logan.day10;

import java.io.Serializable;

public class User implements Serializable {
    String id;
    String name;
    float sal;
    String addr;
    
    public User(){
        
    }
    
    public User(String id, String name, float sal, String addr) {
        super();
        this.id = id;
        this.name = name;
        this.sal = sal;
        this.addr = addr;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public float getSal() {
        return sal;
    }
    public void setSal(float age) {
        this.sal = sal;
    }
    public String getAddr() {
        return addr;
    }
    public void setAddr(String addr) {
        this.addr = addr;
    }
    @Override
    public String toString() {
        return "[id=" + id + ", name=" + name + ", sal=" + sal + ", addr=" + addr + "]";
    }
    
}

 

文件2:ObjectOutputStreamDemo.java 将对象序列化后写入文件

package cn.test.logan.day10;


import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

/**
 * 对象输出流
 * @author QIN
 *
 */
public class ObjectOutputStreamDemo  {
    public static void main(String[] args) throws Exception {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:/obj_out.obj"));
        User u1 = new User("001","范闲",10000f,"China");
        User u2 = new User("002","范健",20000f,"China");
        // 前提是类user是可序列化的
        oos.writeObject(u1);
        oos.writeObject(u2);
        oos.close();
    }
}

 

文件3:ObjectInputStreamDemo.java 将对象反序列化

package cn.test.logan.day10;

import java.io.FileInputStream;
import java.io.ObjectInputStream;


public class ObjectInputStreamDemo {
    public static void main(String[] args) throws Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/obj_out.obj"));
        User u1 = (User)ois.readObject();
        User u2 = (User)ois.readObject();
        System.out.println(u1);
        System.out.println(u2);
ois.close(); } }

 

2.2 复杂对象的序列化与反序列化

2.2.1 ArrayList进行序列化与反序列化

类还是沿用2.1中的User类(注意,必须是可序列化类

package cn.test.logan.day10;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

/**
 * 将ArrayList序列化与反序列化
 * @author QIN
 *
 */
public class ObjectStreamDemo {
    public static void main(String[] args) throws Exception {
        // 创建对象
        User u1 = new User("A01","若若",2800f,"庆国");
        User u2 = new User("A02","朵朵",3800f,"齐国");
        // 新建ArrayList
        ArrayList<User> uList = new ArrayList<>();
        
        // 对象存入到ArrayList中
        uList.add(u1);
        uList.add(u2);
        
        // 将数据存入文件:序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/user.list"));
        oos.writeObject(uList);
        oos.close();
        
        // 将存入的数据取出来:反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/user.list"));
        // 强转型
        ArrayList<User> UserList = (ArrayList<User>) ois.readObject();
        System.out.println(UserList);
    }

}

 

2.2.1 HashMap进行序列化与反序列化

package cn.test.logan.day10;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

/**
 * 将HashMap序列化与反序列化
 * @author QIN
 *
 */
public class ObjectStreamDemo2 {
    public static void main(String[] args) throws Exception {
        // 创建对象
        User u1 = new User("A01","若若",2800f,"庆国");
        User u2 = new User("A02","朵朵",3800f,"齐国");
        //创建map
        HashMap<String, User> umap = new HashMap<>();
        
        umap.put(u1.getName(), u1);
        umap.put(u2.getName(), u2);
        
        // 将数据存入文件:序列化
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/user.list"));
        oos.writeObject(umap);
        oos.close();
        
        // 将存入的数据取出来:反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:/user.list"));
        HashMap<String, User> usermap = (HashMap<String, User> )ois.readObject();
        System.out.println(usermap);
     ois.close(); } }
posted @ 2019-12-29 09:59  OLIVER_QIN  阅读(284)  评论(0编辑  收藏  举报