码迷,mamicode.com
首页 > 编程语言 > 详细

java序列化

时间:2015-07-19 23:18:49      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

public class Test6 implements Serializable{
	private static final long serialUID = 1L;
	private Date loggingDate = new Date();
	private String uid ;
	private transient String pwd ;
	
	Test6(String user ,String pwd ){
		this.uid = user ;
		this.pwd = pwd; 
	}
	
	public String toString (){
		String password = null ;
		if (pwd== null){
			password = "NOT SET";
		}else {
			password = pwd;
		}
		return "logon info:"+uid+","+password;
	}
	public static void main(String [] args){
		Test6 logInfo = new Test6("lsj", "12345");
		System.out.println(logInfo.toString()) ;
		
		try {
			ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("logInfo.out"));
			os.writeObject(logInfo);
			os.close() ;
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		try {
			ObjectInputStream is = new ObjectInputStream(new FileInputStream("logInfo.out"));
			Test6 logInfo1= (Test6) is.readObject();
			System.out.println(logInfo1.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}

 结果

logon info:lsj,12345
logon info:lsj,NOT SET

java序列化

标签:

原文地址:http://www.cnblogs.com/chuiyuan/p/4659954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!