我正在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();
但它无法读取数据。
您是否将每个等级的得分存储在记录中?如果是,则表示您必须获得存储在记录中的最后一个等级和分数。我对吗? – 2012-08-06 07:37:01