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)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 中江县| 时尚| 博罗县| 宜良县| 临泉县| 西乡县| 兴国县| 中方县| 镇坪县| 安多县| 洪雅县| 大埔县| 建阳市| 庄河市| 日喀则市| 会同县| 余干县| 平罗县| 和平县| 双辽市| 饶平县| 麻阳| 靖安县| 泾阳县| 积石山| 乌拉特前旗| 白山市| 新安县| 石渠县| 筠连县| 竹北市| 双鸭山市| 驻马店市| 北流市| 禄丰县| 清新县| 南乐县| 德安县| 吉林省| 白朗县| 韶山市|