IO---object stream

object stream


类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。可序列化类的所有子类型本身都是可序列化的。序列化接口没有方法或字段,仅用于标识可序列化的语义。通俗来说就是将数据结构或对象转换成二进制串的过程。

ObjectOutputStream


writeObject 方法用于将对象写入流中。所有对象(包括 String 和数组)都可以通过 writeObject 写入。

eg:

    public class Person implements Serializable{

    private static final long serialVersionUID = 3851308503396805613L;
    private String userName;
    transient private String password;
    private static String sex;
    
    public Person(String userName,String password,String sex) {
        this.userName = userName;
        this.password = password;
        this.sex = sex;
    }
    
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
    @Override
    public String toString() {
        return "Person userName:"+userName +"  password:"+password + " sex:"+sex;
    }

    public static String getSex() {
        return sex;
    }

    public static void setSex(String sex) {
        Person.sex = sex;
    }

}

public class ObjectTest {
    
    public static void main(String[] args) throws IOException {
        OutputStream os = new FileOutputStream(new File("d:/blog/object.tmp"));
        ObjectOutputStream oos = new ObjectOutputStream(os);
        Person p = new Person("testUser","test1234","man"); 
        oos.writeObject(p);
        oos.close();
    }
}

ObjectInputStream


readObject 方法用于从流读取对象。应该使用 Java 的安全强制转换来获取所需的类型。在 Java 中,字符串和数组都是对象,所以在序列化期间将其视为对象。读取时,需要将其强制转换为期望的类型。 

eg:

InputStream is = new FileInputStream(new File("d:/blog/object.tmp"));
ObjectInputStream ois = new ObjectInputStream(is);
p.setUserName("testTest");
p.setSex("woman");
p = (Person)ois.readObject();
System.out.println(p.toString());

Summarize

When deserializing, the system will detect the serialVersionUID in the file to determine whether it is consistent with the serialVersionUID of the current class. If it is consistent, it means that the version of the serialized class is the same as the current class version, and the deserialization can be successful, otherwise it will fail.
Once a variable is modified with transient, the variable is no longer part of the object's persistence, and the content of the variable cannot be accessed after serialization. A static variable cannot be serialized whether it is modified by transient or not.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325178510&siteId=291194637