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

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


          網(wǎng)站導航:
           
          人人游戲網(wǎng) 軟件開發(fā)網(wǎng) 貨運專家
          主站蜘蛛池模板: 景洪市| 茶陵县| 德安县| 盘锦市| 龙岩市| 桂东县| 特克斯县| 岳阳市| 烟台市| 托克托县| 嫩江县| 芜湖市| 西华县| 偃师市| 平潭县| 高州市| 望奎县| 东丽区| 乌兰县| 资源县| 宝丰县| 四平市| 延安市| 九寨沟县| 肥西县| 宁晋县| 博兴县| 屏东市| 洪湖市| 平江县| 福鼎市| 茶陵县| 黄平县| 北京市| 遂昌县| 五大连池市| 安康市| 三明市| 金溪县| 长宁区| 霍林郭勒市|