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

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 柏乡县| 赣州市| 连平县| 松溪县| 长海县| 肇源县| 赤水市| 浏阳市| 瑞安市| 武定县| 武山县| 四平市| 舒城县| 麻城市| 商洛市| 宁强县| 景谷| 和田市| 江孜县| 霍城县| 侯马市| 衡南县| 南川市| 上饶市| 宁波市| 偏关县| 禄丰县| 铜山县| 健康| 西平县| 上高县| 清涧县| 临安市| 德格县| 高雄市| 黄梅县| 武鸣县| 辉南县| 桦甸市| 汨罗市| 蕉岭县|