posts - 241,  comments - 116,  trackbacks - 0

          transient 關鍵字表示在Serializable 的時候不保存該值。

          //LoggingInfo.java

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


          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;

              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

                  }
              }
          }

          posted on 2011-10-14 10:02 墻頭草 閱讀(446) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 榆树市| 太白县| 班戈县| 外汇| 广丰县| 锦州市| 霍林郭勒市| 嘉义市| 福海县| 孟连| 揭西县| 天水市| 巴塘县| 商河县| 辽宁省| 松潘县| 鲁山县| 肃宁县| 远安县| 岐山县| 大港区| 林州市| 平凉市| 宜兰县| 北票市| 政和县| 师宗县| 谢通门县| 保亭| 沁水县| 威宁| 刚察县| 鲁甸县| 凭祥市| 双流县| 兴业县| 尉氏县| 祁门县| 昭苏县| 建德市| 体育|