设为首页 加入收藏

TOP

Java对象的串行化(Serialization)(二)
2015-07-20 12:52:36 来源: 作者: 【 】 浏览:35
Tags:Java 对象 串行 Serialization
e;
public class LoggingInfo implements java.io.Serializable {
? ? private static final long serialVersionUID = 1L;
? ? private Date loggingDate = new Date();
? ? private String uid;
? ? private transient String pwd;
? ? public void 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;
? ? }


? ? public static void main(String[] args) {
? ? ? ? LoggingInfo logInfo = new LoggingInfo("MIKE", "MECHANICS");
? ? ? ? System.out.println(logInfo.toString());
? ? ? ? try {
? ? ? ? ? ? ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
"logInfo.out"));
? ? ? ? ? ? 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.out"));
? ? ? ? ? ? LoggingInfo logInfo1 = (LoggingInfo) in.readObject();
? ? ? ? ? ? System.out.println(logInfo1.toString());
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? // deal with exception
? ? ? ? }
? ? }
}


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java关键字浅析之native 下一篇Java关键字浅析之final

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: