骑猪闯天下

J2ME随笔,记录成长的脚步

统计

留言簿(3)

阅读排行榜

评论排行榜

[J2ME-原创] RMS 序列化反序列化的用法

        RMS中经常用到格式转换,即序列化反序列化,经常混淆,现在似乎搞明白了,但总还是感觉没有彻底的理解底层的一些东西,虽然不是彻底的理解,还是现单独开篇,把已经理解的给与整理,以备后用,同时欢迎高手指点下ByteArrayOutputStrema、DataOutputStream的真正区别,本质性的,最好能仔细的说明下,为什么需要转换两次。小结如下:

1.    格式转换的序列化反序列化
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() {
        
// 自动生成的构造函数,好像没有用处
    }

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

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

    
    
//获得移动电话号码
    public String getMobilePhone(){
        
return mobilePhone;
    }

    
    
//设置移动电话号码
    public void setMobilePhone(String mobilePhone){
        
this.mobilePhone = mobilePhone;
    }

    
    
//获得家庭电话号码
    public String getPhone(){
        
return phone;
    }

    
    
//设置家庭号码
    public void setPhone(String phone){
        
this.phone = phone;
    }

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

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

    
    
/**
     * 1.Data指的是Java的基本数据类型和String。基本数据类型包括byte、int、char、long、float、double、boolean和short。
     * 2.baos作为参数传送给构造器,这样就可以对基本数据类型以及String进行读写操作
     * 3.关于流的概念参考笔记第一篇。
     
*/

    
    
//把记录卡片中4个属性字符串 转换为 字节数组
    public byte[] serialize() throws IOException {   
        
        
//创建字节数组输出流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
//创建数据输出流,将数据(java基本数据类型和String) 写入指定基础输出流, 
        
//即:DataOutputStream 但是RMS是以字节数组存储的,所以要转换成字节数组。
        DataOutputStream dos = new DataOutputStream(baos);
        
        
//根据所要储存的数据格式分别存入缓冲区内
        dos.writeUTF(userName);
        dos.writeUTF(mobilePhone);        
        dos.writeUTF(phone);
        dos.writeUTF(email);
        
        baos.close();
        dos.close();
        
        
//复制内存中的数据,以字节数组的形式返回此输出流的当前内容
        return baos.toByteArray();      
    }

    
    
//把字节数组 转换为 记录卡片上的四个属性的字符串
    public static BookAccount deserialize(byte[] data) throws IOException {
        
        ByteArrayInputStream bais 
= new ByteArrayInputStream(data);
        DataInputStream dis 
= new DataInputStream(bais);
        
//实例化一个对象
        BookAccount account = new BookAccount();
        
        
//按照存储数据的顺序,把字节数组转换成数据,顺序一定一致。
        account.userName = dis.readUTF();        
        account.mobilePhone 
= dis.readUTF();
        account.phone 
= dis.readUTF();
        account.email 
= dis.readUTF();
        
        
//释放资源
        bais.close();
        dis.close();
        
        
//返回一个account对象        
        return account;      
    }

    
    
/**
     * 2进制数据和字符串的匹配函数
     * matches方法的使用,下面单独介绍。
     
*/

    
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 骑猪闯天下 阅读(557) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: