騎豬闖天下

          J2ME隨筆,記錄成長(zhǎng)的腳步

          統(tǒng)計(jì)

          留言簿(3)

          閱讀排行榜

          評(píng)論排行榜

          [J2ME-原創(chuàng)] RMS 序列化反序列化的用法

                  RMS中經(jīng)常用到格式轉(zhuǎn)換,即序列化反序列化,經(jīng)常混淆,現(xiàn)在似乎搞明白了,但總還是感覺(jué)沒(méi)有徹底的理解底層的一些東西,雖然不是徹底的理解,還是現(xiàn)單獨(dú)開篇,把已經(jīng)理解的給與整理,以備后用,同時(shí)歡迎高手指點(diǎn)下ByteArrayOutputStrema、DataOutputStream的真正區(qū)別,本質(zhì)性的,最好能仔細(xì)的說(shuō)明下,為什么需要轉(zhuǎn)換兩次。小結(jié)如下:

          1.    格式轉(zhuǎn)換的序列化反序列化
          package PhoneBook;

          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.DataInputStream;
          import java.io.DataOutputStream;
          import java.io.IOException;

          public class BookAccount {

              
          private String userName = "";
              
          private String mobilePhone = "";
              
          private String email = "";
              
          private String phone = "";
              
              
          public BookAccount (String userName, String mobilePhone, String phone, String email){
                  
          this.userName = userName;
                  
          this.mobilePhone = mobilePhone;        
                  
          this.phone = phone;
                  
          this.email = email;
              }

              
              
          public BookAccount() {
                  
          // 自動(dòng)生成的構(gòu)造函數(shù),好像沒(méi)有用處
              }

              
              
          //獲得Email
              public String getEmail(){
                  
          return email;
              }

              
              
          //設(shè)置Email
              public void setEmail(String email){
                  
          this.email = email;
              }

              
              
          //獲得移動(dòng)電話號(hào)碼
              public String getMobilePhone(){
                  
          return mobilePhone;
              }

              
              
          //設(shè)置移動(dòng)電話號(hào)碼
              public void setMobilePhone(String mobilePhone){
                  
          this.mobilePhone = mobilePhone;
              }

              
              
          //獲得家庭電話號(hào)碼
              public String getPhone(){
                  
          return phone;
              }

              
              
          //設(shè)置家庭號(hào)碼
              public void setPhone(String phone){
                  
          this.phone = phone;
              }

              
              
          //獲得姓名
              public String getUserName(){
                  
          return userName;
              }

              
              
          //設(shè)置姓名
              public void setUserName(String userName){
                  
          this.userName = userName;
              }

              
              
          /**
               * 1.Data指的是Java的基本數(shù)據(jù)類型和String。基本數(shù)據(jù)類型包括byte、int、char、long、float、double、boolean和short。
               * 2.baos作為參數(shù)傳送給構(gòu)造器,這樣就可以對(duì)基本數(shù)據(jù)類型以及String進(jìn)行讀寫操作
               * 3.關(guān)于流的概念參考筆記第一篇。
               
          */

              
              
          //把記錄卡片中4個(gè)屬性字符串 轉(zhuǎn)換為 字節(jié)數(shù)組
              public byte[] serialize() throws IOException {   
                  
                  
          //創(chuàng)建字節(jié)數(shù)組輸出流
                  ByteArrayOutputStream baos = new ByteArrayOutputStream();
                  
          //創(chuàng)建數(shù)據(jù)輸出流,將數(shù)據(jù)(java基本數(shù)據(jù)類型和String) 寫入指定基礎(chǔ)輸出流, 
                  
          //即:DataOutputStream 但是RMS是以字節(jié)數(shù)組存儲(chǔ)的,所以要轉(zhuǎn)換成字節(jié)數(shù)組。
                  DataOutputStream dos = new DataOutputStream(baos);
                  
                  
          //根據(jù)所要儲(chǔ)存的數(shù)據(jù)格式分別存入緩沖區(qū)內(nèi)
                  dos.writeUTF(userName);
                  dos.writeUTF(mobilePhone);        
                  dos.writeUTF(phone);
                  dos.writeUTF(email);
                  
                  baos.close();
                  dos.close();
                  
                  
          //復(fù)制內(nèi)存中的數(shù)據(jù),以字節(jié)數(shù)組的形式返回此輸出流的當(dāng)前內(nèi)容
                  return baos.toByteArray();      
              }

              
              
          //把字節(jié)數(shù)組 轉(zhuǎn)換為 記錄卡片上的四個(gè)屬性的字符串
              public static BookAccount deserialize(byte[] data) throws IOException {
                  
                  ByteArrayInputStream bais 
          = new ByteArrayInputStream(data);
                  DataInputStream dis 
          = new DataInputStream(bais);
                  
          //實(shí)例化一個(gè)對(duì)象
                  BookAccount account = new BookAccount();
                  
                  
          //按照存儲(chǔ)數(shù)據(jù)的順序,把字節(jié)數(shù)組轉(zhuǎn)換成數(shù)據(jù),順序一定一致。
                  account.userName = dis.readUTF();        
                  account.mobilePhone 
          = dis.readUTF();
                  account.phone 
          = dis.readUTF();
                  account.email 
          = dis.readUTF();
                  
                  
          //釋放資源
                  bais.close();
                  dis.close();
                  
                  
          //返回一個(gè)account對(duì)象        
                  return account;      
              }

              
              
          /**
               * 2進(jìn)制數(shù)據(jù)和字符串的匹配函數(shù)
               * matches方法的使用,下面單獨(dú)介紹。
               
          */

              
          public static boolean matches(byte[] data, String userName) throws IOException{
                  ByteArrayInputStream bais 
          = new ByteArrayInputStream(data);
                  DataInputStream dis 
          = new DataInputStream(bais);
                  
          try{
                      
          if ( (dis.readUTF() ).equals(userName) )
                      
          return true;            
                  }
          catch (IOException e){
                      e.printStackTrace();
                  }
                  
                  
          return false;        
              }


          }



          <End>

          posted on 2008-08-30 21:20 騎豬闖天下 閱讀(570) 評(píng)論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 关岭| 上杭县| 洮南市| 平陆县| 潜山县| 故城县| 邵武市| 隆德县| 东明县| 青阳县| 九江县| 桦南县| 九台市| 揭阳市| 彭泽县| 班玛县| 台安县| 横山县| 德安县| 武清区| 永春县| 循化| 延庆县| 彝良县| 武平县| 武陟县| 清涧县| 潞西市| 彩票| 巴林左旗| 建瓯市| 尉氏县| 郁南县| 渝中区| 图木舒克市| 宁晋县| 子长县| 惠东县| 金坛市| 印江| 荣昌县|