RecordStore是已byte陣列存儲(chǔ)的.所以需要將整個(gè)物件序列化成byte 陣列存入紀(jì)錄倉(cāng)儲(chǔ),也可以從資料倉(cāng)儲(chǔ)之中讀入一個(gè)byte 陣列,然後將其回復(fù)成原本物件內(nèi)部的狀態(tài)。
在此我們要借助四個(gè)類別的協(xié)助,他們分別是:
ByteArrayOutputStream、ByteArrayInputStream、
DataOutputStream、DataInputStream。
轉(zhuǎn)換例子如下:
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)
{
}
}
}
在此我們要借助四個(gè)類別的協(xié)助,他們分別是:
ByteArrayOutputStream、ByteArrayInputStream、
DataOutputStream、DataInputStream。
轉(zhuǎn)換例子如下:




































































