`
metallica_1860
  • 浏览: 32327 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

transient

    博客分类:
  • Java
阅读更多

package com.pp.test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

/**
 * 
 * @author <a href="mailto:zhangjun@pegasus-se.com">ZhangJun</a>
 * @date 2010/05/14
 */
public class Logon implements Serializable {
	private static final long serialVersionUID = 4274726103149096976L;
	/***/
	private Date date = new Date();
	/***/
	private String username;
	/** transient 是用于声明序列化的时候不被存储的 */
	private transient String password;

	Logon(String name, String pwd) {
		this.username = name;
		this.password = pwd;
	}

	public String toString() {
		return "logon info: \n " + "username: " + this.username + "\n date: "
				+ this.date.toString() + "\n password: " + this.password;
	}

	public static void main(String[] args) {
		Logon logon = new Logon("junzi", "password");
		System.out.println(logon);
		try {
			ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
					"Logon.out"));
			o.writeObject(logon);
			o.close();
			// 
			int seconds = 5;
			long t = System.currentTimeMillis() + seconds * 1000;
			while (System.currentTimeMillis() < t)
				;
			// 
			ObjectInputStream in = new ObjectInputStream(new FileInputStream(
					"Logon.out"));
			System.out.println("========================================");
			System.out.println("Recovering object at " + new Date());
			System.out.println("========================================");
			logon = (Logon) in.readObject();
			System.out.println(logon);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
分享到:
评论
1 楼 metallica_1860 2010-05-14  
logon info:
username: junzi
date: Fri May 14 10:31:50 CST 2010
password: password
========================================
Recovering object at Fri May 14 10:31:55 CST 2010
========================================
logon info:
username: junzi
date: Fri May 14 10:31:50 CST 2010
password: null

相关推荐

Global site tag (gtag.js) - Google Analytics