2012-08-05 28 views
3

我正在J2ME中做一个游戏。 我想保存数据层面和RMS得分。这是我的代码 这是类RMSData:在j2me中读写RMS

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(); 

但它无法读取数据。

+0

您是否将每个等级的得分存储在记录中?如果是,则表示您必须获得存储在记录中的最后一个等级和分数。我对吗? – 2012-08-06 07:37:01

回答

0

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

  • 经历的所有记录。
  • 阅读所有入一个字节数组一次一个
  • 丢弃所有的数据,除非它是在店内
  • 回的最后一条记录包含最后一个记录的字符串。

每次打电话给readRecStore时,你有可能得到相同的结果(分数)吗?

+0

st =“Level:”+ RMSData.readRecStore()+“Score:”+ RMSData.readRecStore();将返回一个结果的水平和分数。但我不知道读有效值返回两个结果的水平和分数。请帮助我 – 2012-08-06 07:05:45

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; 
    }