Well, I have not passed the code below through a compiler, so it can contain errors, but it looks like:
Code:
public void saveGame(int level) throws Exception {
RecordStore rs = RecordStore.openRecordStore("MY_GAME",true);
ByteArrayOutputStream baos = baos = new ByteArrayOutputStream();
DataOutputStream dos = dos = new DataOutputStream(baos);
dos.writeInt(level);
byte[] data = baos.toByteArray();
if(rs.getNumRecords() == 0) {
rs.addRecord(data,0,data.length);
}
else {
rs.setRecord(1,data,0,data.length);
}
}
public int readGameLevel() throws Exception {
RecordStore rs = RecordStore.openRecordStore("MY_GAME",true);
if(rs.getNumRecords() == 0) throw new Exception("No level saved");
byte[] data = rs.getRecord(1); //1 because there's only one record
ByteArrayInputStream baos = baos = new ByteArrayInputStream(data);
DataInputStream dos = dos = new DataInputStream(baos);
int level = dos.readInt();
return level;
}
Hope it helps.
Daniel