【Java】IO流--对象流--ObjectInputStream、ObjectOutputStream

对象流--ObjectInputStream、ObjectOutputStream

作用

ObjectInputStream

将 Java 对象转换成字节序列(IO 字节流)

ObjectOutputStream --> 序列化 --> 写对象,将对象以 “ 二进制/ 字节 ” 的形式写到(文件)

ObjectOutputStream
对象反序列化 (DeSerialization),从字节序列中恢复 Java 对象
ObjectInputStream -->反序列化 -->读对象

序列化原因

序列化以后的对象可以保存到磁盘上,也可以在网络上传输,使得不同的计算机可以共享对象.(序列化的字节序列是平台无关的)

对象序列化的条件

只有实现了 Serializable 接口的类的对象才可以被序列化。Serializable 接口中没有任何的方法,实现该接口的类不需要实现额外的方法。
如果对象的属性是对象,属性对应类也必须实现 Serializable接口

如何实现序列化

创建 ObjectOutputStream 对象
调用 writeObject()输出对象

如何实现反序列化

创建 ObjectInputStream 对象
调用 readObject() 读取对象

代码

目标:将 Person 类的对象p序列化和反序列化

Person 类

import java.io.Serializable;

public class Person implements Serializable{
    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
    
}

TestObejectIO 类

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

public class TestObejectIO {
    public static void main(String[] args) {
        Write();
        Read();
    }
    
    public static void Read() {
        ObjectInputStream ois=null;
        Person p;
        try {
            ois = new ObjectInputStream(new FileInputStream("F://obejecio.txt"));
            p = (Person)ois.readObject();
            System.out.println(p);
            System.out.println(ois.readInt());
            System.out.println(ois.readBoolean());
            System.out.println(ois.readUTF());
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(ois!=null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void Write() {
        Person p=new Person("Nexfia", 20);
        ObjectOutputStream oos=null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("F://obejecio.txt"));
            oos.writeObject(p);
            oos.writeInt(1024);
            oos.writeBoolean(true);
            oos.writeUTF("aaa");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            if(oos!=null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
    }
}
原文地址:https://www.cnblogs.com/syxy/p/12296412.html