您的位置:首页 > 编程语言 > Java开发

java序列化

2015-07-19 22:04 302 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: