Java transient关键字

来源:互联网 发布:舞美酷库软件下载mac 编辑:程序博客网 时间:2024/06/03 21:21
package com.darren.test.serializable;import java.io.Serializable;//Java的serialization提供了一种持久化对象实例的机制。当持久化对象时,可能有一个特殊的对象数据成员,我们不想用serialization机制来保存它。//为了在一个特定对象的一个域上关闭serialization,可以在这个域前加上关键字transient。//transient是Java语言的关键字,用来表示一个域不是该对象串行化的一部分。//当一个对象被串行化的时候,transient型变量的值不包括在串行化的表示中,然而非transient型的变量是被包括进去的。public class Transient implements Serializable {    private static final long serialVersionUID = 8725475784100780639L;    private String userName;    private transient String password;    private transient String description;    public Transient(String userName, String password) {        this.userName = userName;        this.password = password;        this.description = "I'm Darren!";    }    @Override    public String toString() {        if (this.password == null) {            this.password = "NOT SET PASSWORD!";        }        if (this.description == null) {            this.description = "NOT SET DESCRIPTION!";        }        StringBuilder builder = new StringBuilder();        builder.append("User:").append("[");        builder.append("userName = ").append(this.userName).append(", ");        builder.append("password = ").append(this.password).append(", ");        builder.append("description = ").append(this.description);        builder.append("]");        return builder.toString();    }}


package com.darren.test.serializable;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;public class TransientTest {    public static void main(String[] args) {        // 现在我们创建一个这个类的实例,并且串行化(serialize)它 ,然后将这个串行化对象写如磁盘。        Transient t = new Transient("Darren", "123456");        String path = "transient.txt";        System.out.println("写之前:");        System.out.println(t.toString());        write(t, path);        Transient rt = read(path);        System.out.println("写之后:");        System.out.println(rt.toString());        // 运行结果:        // 写之前:        // User:[userName = Darren, password = 123456, description = I'm Darren!]        // 写之后:        // User:[userName = Darren, password = NOT SET PASSWORD!, description = NOT SET DESCRIPTION!]        // 如果我们运行这段代码,我们会注意到从磁盘中读回(read——back)的对象打印password为"NOT SET PASSWORD!"。        // 这是当我们定义password域为transient时,所期望的正确结果。        // 问题:为什么description的值有默认这也不行呢?        // 我们仍然看到读回的对象打印description 为"NOT SET DESCRIPTION!"。        // 当从磁盘中读出某个类的实例时,实际上并不会执行这个类的构造函数,而是载入了一个该类对象的持久化状态,并将这个状态赋值给该类的另一个对象。    }    private static Transient read(String path) {        Transient t = null;        FileInputStream fileIn = null;        ObjectInputStream in = null;        try {            fileIn = new FileInputStream(path);            in = new ObjectInputStream(fileIn);            Object object = in.readObject();            t = (Transient) object;            in.close();        } catch (Exception e) {            e.printStackTrace();        }        return t;    }    private static void write(Transient t, String path) {        FileOutputStream fileOut = null;        ObjectOutputStream out = null;        try {            fileOut = new FileOutputStream(path);            out = new ObjectOutputStream(fileOut);            out.writeObject(t);            out.close();        } catch (Exception e) {            e.printStackTrace();        }    }}


0 0
原创粉丝点击