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 墻頭草 閱讀(449) 評論(0)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 百色市| 青岛市| 丁青县| 三门县| 钟山县| 博爱县| 瑞金市| 山阳县| 乌审旗| 樟树市| 万山特区| 和田市| 丘北县| 呈贡县| 安龙县| 涟源市| 称多县| 图片| 太白县| 澄迈县| 黄冈市| 平和县| 永和县| 西安市| 陆丰市| 泽州县| 东方市| 营口市| 冷水江市| 讷河市| 汶川县| 唐山市| 青阳县| 剑阁县| 吴江市| 濮阳县| 湖北省| 普定县| 富平县| 天峻县| 榆中县|