posts - 241,  comments - 116,  trackbacks - 0
          transient說明一個屬性是臨時的,不會被序列化。
          下面是一個Demo,name聲明為 transient,不被序列化
          package com.zzs.tet;

          import java.io.File;
          import java.io.FileInputStream;JDK源代碼之LinkedList類分析
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.ObjectInput;
          import java.io.ObjectInputStream;
          import java.io.ObjectOutput;
          import java.io.ObjectOutputStream;
          import java.io.Serializable;

          public class TransientDemo implements Serializable{
              /**
               *
               */
              private static final long serialVersionUID = 1L;
              private  transient String name;
              private String password;
              
              public String getName() {
                  return name;
              }

              public void setName(String name) {
                  this.name = name;
              }

              public String getPassword() {
                  return password;
              }

              public void setPassword(String password) {
                  this.password = password;
              }

              /**
               * @param args
               * @throws IOException
               * @throws FileNotFoundException
               * @throws ClassNotFoundException
               */
              public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
                  // TODO Auto-generated method stub
                  String path="D:"+File.separator+"object.txt";
                  File file=new File(path);
                  TransientDemo transientDemo=new TransientDemo();
                  transientDemo.setName("姓名");
                  transientDemo.setPassword("密碼");
                  ObjectOutput output=new ObjectOutputStream(new FileOutputStream(file));
                  output.writeObject(transientDemo);
                  ObjectInput input=new ObjectInputStream(new FileInputStream(file));
                  TransientDemo demo=(    TransientDemo )input.readObject();
                  System.out.println(demo.getName()+demo.getPassword());
              }

          }
          posted on 2011-07-26 09:29 墻頭草 閱讀(596) 評論(0)  編輯  收藏

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


          網站導航:
           
          人人游戲網 軟件開發網 貨運專家
          主站蜘蛛池模板: 碌曲县| 左云县| 浦县| 罗山县| 栖霞市| 巴楚县| 绥中县| 香港 | 重庆市| 陇川县| 桂阳县| 凤山县| 登封市| 灵寿县| 监利县| 兰考县| 北辰区| 德保县| 松原市| 汝城县| 滦南县| 永吉县| 青岛市| 简阳市| 斗六市| 新郑市| 南丰县| 辰溪县| 射洪县| 五华县| 临城县| 静安区| 濮阳市| 南康市| 房山区| 四川省| 清丰县| 沧州市| 老河口市| 平乐县| 光山县|