站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2ME 专题详解RMS(二)

详解RMS(二)

摘要: 详解RMS(二)
作者:

由上面的例子,我们开到了为了读取数据,我们要分配一个足够大的数组,如下面的代码所示:
public int readInt4RS(RecordStore rs,int recordid)
{
int len=10;//rs.getRecordSize(recordid);
byte [] recordData = new byte[len];

try
{
recordData= rs.getRecord(recordid);
}catch(Exception e)
{
}
int result = (Integer.parseInt(new String(recordData)));
return result;
}
根本不能按需分配适当的空间(最起码我没有解决)。

这样我们不管纪录的数据大小都这样分配空间的话,势必对空间造成了浪费。有没有一个更好的方法呢?

java很体贴给我们提供了很多的工具。比如:ByteArrayOutputStream、ByteArrayInputStream、
DataOutputStream、DataInputStream这四个类。
我这里使用王森在《Java 手機程式設計入門》书中提供的一个例子吧。
假设我们要设计一个通讯路程序,里面需要一个资料结构叫做FriendData,其结构如下:
class FriendData
{
String name ;
String tel ;
boolean sex ;
int age ;
}
在这个结构中我们要存储3种不同类型的记录,并且4个段的长度都不相同。为了要将此类型内的数据转换成byte数组或是将byte数组转换为相应的资料类型。我们可以使用上面的4个类来写decode()函数和encode 函数,
其內容如下:
class FriendData
{
String name ;
String tel ;
boolean sex ;
int age ;

public FriendData()
{
name = "NO NAME" ;
name = "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)
{
}
}
}
有了这样一个类我们可以使用下面的方法使用该类:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
import javax.microedition.rms.* ;
public class DataRecordStoreTest extends MIDlet implements ItemStateListener
{
private Display display;

public DataRecordStoreTest()
{
display = Display.getDisplay(this);
}

public void startApp()
{
String dbname = "testdb" ;
Form f = new Form("RS Test") ;
RecordStore rs = openRSAnyway(dbname) ;
if( rs == null )
{
//開啟失敗停止MIDlet
f.append("Table open fail") ;
}else
{
try
{
//构造类
FriendData fd = new FriendData() ;
fd.name = "尾小保" ;
fd.tel = "0805449" ;
fd.sex = false ;
fd.age = 17 ;
//得到数据
byte tmp[] = fd.encode() ;
//存储数据
int id = rs.addRecord(tmp,0,tmp.length) ;
//构造一个类,用来现实数据
FriendData fd2 = new FriendData() ;
//分离数据
fd2.decode(rs.getRecord(id)) ;
//显示
f.append("姓名 :"+ fd2.name) ;
f.append("\n 電話 :"+ fd2.tel) ;
if(fd2.sex)
{
f.append("\n 性別 : 男") ;
}else
{
f.append("\n 性別 : 女") ;
}
f.append("\n 年齡 :"+ fd2.age) ;
rs.closeRecordStore() ;
deleteRS(dbname) ;
}
catch(Exception e)
{
}
}
display.setCurrent(f) ;
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void itemStateChanged(Item item)
{
}
public RecordStore openRSAnyway(String rsname)
{
RecordStore rs = null ;
//名稱大於32 個字元就不接受
if(rsname.length() > 32)
return null ;
try
{
rs = RecordStore.openRecordStore(rsname,true) ;
return rs ;
}catch(Exception e)
{
return null ;
}
}
public boolean deleteRS(String rsname)
{
if(rsname.length() > 32)
return false ;
try
{
RecordStore.deleteRecordStore(rsname) ;
}catch(Exception e)
{
return false ;
}
return true ;
}
}




2007-02-06 08:52 AM

↑返回目录
前一篇: MIDlet 国际化[转贴]
后一篇: 详解RMS(一)