验证transient关键字
来源:互联网 发布:泛雅网络课程 编辑:程序博客网 时间:2024/06/06 03:32
对对象进行序列化时,有些实例域是不需要序列化的,可以在该实例域前加transient关键字
public class LoggingInfo implements java.io.Serializable{
public static void main(String[] args) {
// TODO Auto-generated method stub
LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
System.out.println(logInfo.toString());
try {
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("logInfo"));
o.writeObject(logInfo);//将对象状态写入输出流
o.close();
} catch (Exception e) {// deal with exception
}
// To read the object back, we can write
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("logInfo"));
LoggingInfo logInfo1 = (LoggingInfo) in.readObject();//从输入流读取对象状态
System.out.println(logInfo1.toString());
} catch (Exception e) {// deal with exception
}
}
private static final long serialVersionUID = 1L;
private Date loggingDate = new Date();
private String uid;
private transient String pwd;//不序列化pwd
LoggingInfo(String user, String password) {
uid = user;
pwd = password;
}
public String toString() {
String password = null;
if (pwd == null) {
password = "NOT SET";
} else {
password = pwd;
}
return "logon info: /n " + "user: " + uid + "/n logging date : "
+ loggingDate.toString() + "/n password: " + password;
}
}
运行上面的测试类,输出:
logon info:
user: MIKE
logging date : Tue Jul 20 15:37:20 CST 2010
password: MECHANICS --------------->初始状态正常获取对象的状态值
logon info:
user: MIKE
logging date : Tue Jul 20 15:37:20 CST 2010
password: NOT SET ------------->不能得到对象的状态值,表明pwd未被序列化
总结:通过以上测试可以得知,给实例域加上transient关键字后,序列化该对象时相应的实例域不会被序列化
- 验证transient关键字
- transient 关键字
- transient 关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient关键字
- transient 关键字
- transient关键字
- transient关键字
- transient关键字
- IE下报dojo找不到,Firefox却没有
- FSDK_DetectEyes
- CString常用函数。自己常要查的东西,就转载在这里。
- 个人学习1之-DATETIME数据类型
- 陈灯可重用代码管理器1.0发布(CodeManager_Desktop)
- 验证transient关键字
- open office (ooffice) 如何使用公式编辑器 (formula)
- 关于flex的渐变色
- RMAN 异机 复制数据库
- 调用SetDIBits修改位图内容
- 网页代码中最常见的几路注释方法
- 反对反对撒
- 拆分字段SQL语句
- 1-6例示strerror和perror