RMS存储持久数据的问题

时间:2012-01-05 16:28:34

标签: java java-me nokia midlet rms

1.模拟器上的问题:
我第一次启动我的midlet应用程序是存储一些数据,然后我在第二次重新启动它是读取存储的数据。它在前两种情况下运作良好,没有任何例外。

但是我以相同的方式第二次重新启动它然后它给出了异常:“未捕获的异常java / lang / NumberFormatException:”它只处理char并且总数据小于64 kb。

2.真实设备上的问题:
RMS根本不起作用。我不知道是否需要给手机许可(诺基亚n95)。

感谢。


在应用程序中,它只根据所选国家/地区将慈善公司存储到公司。因此,如果已选择国家/地区,则必须跳过国家/地区列表,然后在每次重新启动时显示公司列表。 在下面的代码中,rms_Check()方法是检查数据以打开国家或公司列表框架。

public class X {

private static RecordStore rs =null;
private static Vector rms_Vector = new Vector();
static final String REC_STORE ="db_1";

public X() {

}

public void openRecStore(){
    try {
        rs = RecordStore.openRecordStore(REC_STORE, true);
        System.out.println("open record store");
    } catch (Exception e)
    {
        db(e.toString()+" in openRecStore");
    }
}

public void closeRecStore(){
    try {
        rs.closeRecordStore();
    } catch (Exception e) {
        db(e.toString()+" in closeRecStore");
    }
}

public void deleteRecStore()
{
if (RecordStore.listRecordStores()!=null){
    try {
        RecordStore.deleteRecordStore(REC_STORE);
    } catch (Exception e) {
        db(e.toString()+" in deleteRecStore");
    }
}
}

public void writeRecord(String str) throws UnsupportedEncodingException
{
    byte[] rec = str.getBytes("UTF-8");

    try {
        rs.addRecord(rec, 0, rec.length);
        System.out.println("write record store");
    } catch (Exception e) {
        db(e.toString()+" in writeRecord");
    }
}

public void readRecord()
{
    try {
        // Intentionally it is too small to test code 
        byte[] m_enc = new byte[5];
        byte[] recData = new String(m_enc).getBytes("UTF-8");
        int len;

        for(int i =1; i<= rs.getNumRecords(); i++){
        if(rs.getRecordSize(i)> recData.length)
            recData = new byte[rs.getRecordSize(i)];

        len = rs.getRecord(i, recData, 0);
        System.out.println("Record #"+i+":"+new String(recData, 0, len));
        System.out.println("------------------------");
        rms_Vector.addElement(new String(recData, 0, len));
        }
    } catch (Exception e) {
        db(e.toString() +" in readStore");
    }
}

private void db(String str)
{
System.out.println("Msg:"+str);
}
public Vector rms_Array(){

    return this.rms_Vector;
}

public boolean rms_Check(){
if(this.rms_Vector.size()>0){
    System.out.print("rms_check: true");
    // if true it will display company list every time
return true;
}else{
    System.out.print("rms_check: false");
    //if false it will display country list then company list
return false;
}
}

}

1 个答案:

答案 0 :(得分:1)

使用此

private RecordStore rs = null;    // Record store
public String REC_STORE = "RSM name"; // Name of record store
public int record_max=0;

public void openRecStore(){
try{
rs = RecordStore.openRecordStore(REC_STORE, true );
}catch (Exception e){}
}  

 public void closeRecStore(){
try{
rs.closeRecordStore();
}catch (Exception e){}
}

public void deleteRecStore(){
if (RecordStore.listRecordStores() != null){
try{
RecordStore.deleteRecordStore(REC_STORE);
}catch (Exception e){}
}  
}

public void writeRecord(String str){
byte[] rec = str.getBytes();

 try{
 rs.addRecord(rec, 0, rec.length);
 }catch (Exception e){}
 }

  public void readRecords(){
   try{
   byte[] recData = new byte[5]; 
    int len;
    record_max=rs.getNumRecords();
    for(int i = 1; i <= record_max; i++){

        if(rs.getRecordSize(i) > recData.length){
        recData = new byte[rs.getRecordSize(i)];

        }
        len = rs.getRecord(i, recData, 0);
        file_name[i]=new String(recData, 0, len);

       }
     }catch (Exception e){}
   }

你有file_name []数组保存数据

用于加载肌动蛋白commad使用:

            openRecStore();
            readRecords();
            for(int j=1;j<=record_max;j++ ) {
             System.out.println("Record " + j + " : " + file_name[j]);
                }
             closeRecStore();

并保存:

            openRecStore();
            writeRecord(textField.getString());
            closeRecStore();