konhon

          忘掉過去,展望未來。找回自我,超越自我。
          逃避不一定躲的過, 面對不一定最難過, 孤單不一定不快樂, 得到不一定能長久, 失去不一定不再擁有, 可能因為某個理由而傷心難過, 但我卻能找個理由讓自己快樂.

          Google

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            203 Posts :: 0 Stories :: 61 Comments :: 0 Trackbacks
          RecordStore是已byte陣列存儲的.所以需要將整個物件序列化成byte 陣列存入紀錄倉儲,也可以從資料倉儲之中讀入一個byte 陣列,然後將其回復成原本物件內部的狀態。
          在此我們要借助四個類別的協助,他們分別是:
          ByteArrayOutputStream、ByteArrayInputStream、
          DataOutputStream、DataInputStream。

          轉換例子如下:
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.DataInputStream;
          import java.io.DataOutputStream;

          public class FriendData {
              String name;

              String tel;

              boolean sex;

              
          int age;

              
          public FriendData() {
                  name 
          = "NO NAME";
                  tel 
          = "NO TEL";
                  sex 
          = false;
                  age 
          = 0;
              }


              
          public byte[] encode() {
                  
          byte[] result = null;
                  
          try {
                      ByteArrayOutputStream bos 
          = new ByteArrayOutputStream();
                      DataOutputStream dos 
          = new DataOutputStream(bos);
                      dos.writeUTF(name);
                      dos.writeUTF(tel);
                      dos.writeBoolean(sex);
                      dos.writeInt(age);
                      result 
          = bos.toByteArray();
                      dos.close();
                      bos.close();
                  }
           catch (Exception e) {
                  }

                  
          return result;
              }


              
          public void decode(byte[] data) {
                  
          try {
                      ByteArrayInputStream bis 
          = new ByteArrayInputStream(data);
                      DataInputStream dis 
          = new DataInputStream(bis);
                      name 
          = dis.readUTF();
                      tel 
          = dis.readUTF();
                      sex 
          = dis.readBoolean();
                      age 
          = dis.readInt();
                      dis.close();
                      bis.close();
                  }
           catch (Exception e) {
                  }

              }

          }

          posted on 2005-09-02 02:21 konhon 優華 閱讀(637) 評論(0)  編輯  收藏 所屬分類: J2me
          主站蜘蛛池模板: 辉南县| 镇原县| 榆社县| 民权县| 顺义区| 达孜县| 大港区| 翁牛特旗| 永德县| 锡林郭勒盟| 喜德县| 白银市| 瓦房店市| 威信县| 上林县| 烟台市| 云霄县| 祁门县| 黄骅市| 金秀| 竹北市| 兴文县| 孝感市| 东光县| 桑植县| 龙游县| 武定县| 玉环县| 尖扎县| 溧阳市| 林周县| 科技| 汤原县| 娄底市| 河曲县| 公主岭市| 沅江市| 丰顺县| 壤塘县| 贵阳市| 海口市|