3

I am doing a game in J2ME . I want save data level and score in RMS .This is my code This is class 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;
   }
}

I write level and score to RMS :

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

And then I read it :

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

But it can't read the data.

Mun0n
  • 4,438
  • 4
  • 28
  • 46
Khiem Khiem
  • 107
  • 3
  • 8

2 Answers2

2

Verify its writeRecStore method, if you already have the first record saved only need to update their values ​​with setRecord. See an example we use here:

   /**
     * 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;
    }
Douglas Frari
  • 4,167
  • 2
  • 22
  • 23
0

The way I read it, I think that what RMSData.readRecStore() does is:

  • go through all the records.
  • read them all into a byte array one at a time
  • discard all the data unless it is the last record in the store
  • return a String containing that last record.

Any chance you get the same result (the score) every time you call readRecStore?

michael aubert
  • 6,836
  • 1
  • 16
  • 32
  • st = "Level : "+RMSData.readRecStore()+"Score : "+RMSData.readRecStore(); will returns one result for Level and Score .But I don't known read RMS to return two result for level and score .Please help me – Khiem Khiem Aug 06 '12 at 07:05