在j2me中读写RMS

时间:2012-08-05 10:47:30

标签: java-me j2mepolish rms

我在J2ME做游戏。 我希望在RMS中保存数据级别和分数。这是我的代码 这是类RMSData:

public class RMSData {

private static RecordStore rs = null;
static final String REC_STORE = "ReadWriteRMS";
public static void openRecStore(){
    try {
        rs = RecordStore.openRecordStore(REC_STORE, true);
    } catch (Exception e) {} 
}   
public static void closeRecStore(){
    try {
        rs.closeRecordStore();
    } catch (Exception e) {}
}

public static void deleteRecStore(){
    if(RecordStore.listRecordStores()!= null){
        try {
            RecordStore.deleteRecordStore(REC_STORE);
        } catch (Exception e) {}
    }
}   
public static void writeRecStore(String str){
    byte[] rec = str.getBytes();
    try {
        rs.addRecord(rec, 0, rec.length);
    } catch (Exception e) {}
}
public static String readRecStore(){
     String kq =null; 
     try{
      byte[] recData = new byte[5]; 
      int len;

      if (rs.getNumRecords()==0) return null; 

      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); 
      if (i==rs.getNumRecords()) kq = new String(recData, 0, len);
      }
      }catch (Exception e){}
      return kq;
   }
}

我写水平并得分为RMS:

RMSData.openRecStore();
RMSData.writeRecStore(String.valueOf(LevelPlay));
RMSData.writeRecStore(String.valueOf(Score));           
RMSData.closeRecStore();

然后我读了它:

RMSData.openRecStore();
String st = null;       
if(RMSData.readRecStore() == null)
    st = "Level : 0"+"Score : 0";
else
    st = "Level : "+RMSData.readRecStore()+"Score : "+RMSData.readRecStore();
RMSData.closeRecStore();

但它无法读取数据。

2 个答案:

答案 0 :(得分:2)

验证其writeRecStore方法,如果已经保存了第一条记录,则只需要使用setRecord更新它们的值。请参阅我们在此处使用的示例:

   /**
     * Saves the current data.
     * 
     */
    public static void saveConfiguration() {
        // ...

        RecordStore recordStore = open(CONFIG_DB);

        if (recordStore != null) {
            byte byteArray[] = Persistence.packConfiguration();
            try {
                if (recordStore.getNumRecords() == 2) {
                    byte roolback[] = recordStore.getRecord(CURRENT);
                    recordStore.setRecord(ROLLBACK, roolback, 0, roolback.length);
                    recordStore.setRecord(CURRENT, byteArray, 0, byteArray.length);
                } else {
                    recordStore.addRecord(byteArray, 0, byteArray.length);
                    recordStore.addRecord(byteArray, 0, byteArray.length);
                }

            } catch (Exception ex) {

                ex.printStackTrace();
            }
        }

        Persistence.close(recordStore);

    }



    /**
     * Packs the data and stores memory data into a byte array and returns it.
     * 
     * @return The array of bytes with application data packed.
     */
    private static byte[] packConfiguration() {
        byte byteArray[] = null;

        try {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);

            // starts packing the data into the byte array
            dataoutputstream.writeBoolean(Persistence.rememberLogin);
            dataoutputstream.writeBoolean(Persistence.sounds);
            dataoutputstream.writeInt(Persistence.fontSize);
            dataoutputstream.writeInt(Persistence.idiom);

            dataoutputstream.writeUTF((Persistence.msg == null ? "" : Persistence.msg));
            dataoutputstream.writeInt(Persistence.availability);

            // ...

            // end of packing
            byteArray = bytearrayoutputstream.toByteArray();
            dataoutputstream.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return byteArray;
    }

答案 1 :(得分:0)

我读它的方式,我认为RMSData.readRecStore()的作用是:

  • 查看所有记录。
  • 一次一个地将它们全部读入一个字节数组
  • 丢弃所有数据,除非它是商店中的最后一条记录
  • 返回包含最后一条记录的字符串。

每次调用readRecStore都会得到相同的结果(得分)吗?