📄 recordstoregamedata.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import java.util.Random;
public class RecordStoreGameData extends MIDlet {
private RecordStore rs;
private Display display;
public RecordStoreGameData() {
super();
display = Display.getDisplay(this);
}
protected void startApp() throws MIDletStateChangeException {
String dbname = "db";//数据仓库的名字
Form f = new Form("游戏高分榜");
rs = openRSAnyway(dbname);
if (rs == null) {
f.append("Table open fail");
} else {
try {
addData();
sortData(f);
rs.closeRecordStore();
deleteRS(dbname);
} catch (Exception e) {
}
}
display.setCurrent(f);
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
public RecordStore openRSAnyway(String rsname) {
RecordStore rs = null;
// 名稱大於32 個字元就不接受
if (rsname.length() > 32)
return null;
try {
rs = RecordStore.openRecordStore(rsname, true);
return rs;
} catch (Exception e) {
return null;
}
}
public void addData() {//在数据仓库中增加10条随机数据,删除第4条记录
try {
for (int i = 0; i < 10; i++) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
DataOutputStream doSteam = new DataOutputStream(bo);
doSteam.writeUTF("player" + i);
Random random = new Random();
int record = Math.abs(random.nextInt() % 100);
doSteam.writeInt(record);
byte data[] = bo.toByteArray();
rs.addRecord(data, 0, data.length);
doSteam.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void sortData(Form form) {//对数据进行排序
RecordComparator recordComparator = new RecordComparator() {//排序
public int compare(byte[] firstdata, byte[] seconddata) {
DataInputStream firstSteam = new DataInputStream(
new ByteArrayInputStream(firstdata));
DataInputStream secondSteam = new DataInputStream(
new ByteArrayInputStream(seconddata));
int firstRecord = 0;
int secondRecord = 0;
try {
firstSteam.readUTF();//跳过姓名
firstRecord = firstSteam.readInt();
secondSteam.readUTF();//跳过姓名
secondRecord = secondSteam.readInt();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (firstRecord == secondRecord) {
return RecordComparator.EQUIVALENT;
} else {
if (firstRecord < secondRecord) {
return RecordComparator.FOLLOWS;
} else {
return RecordComparator.PRECEDES;
}
}
}
};
try {
RecordEnumeration recordEnumeration = rs.enumerateRecords(null,
recordComparator, false);
int i = 0;
while (recordEnumeration.hasNextElement()) {
byte[] temp = recordEnumeration.nextRecord();
DataInputStream stream = new DataInputStream(
new ByteArrayInputStream(temp));
if (i < 5) {//只取前五名
form.append("姓名: " + stream.readUTF() + " ");
form.append("得分: " + stream.readInt() + "\n");
}
i++;
stream.close();
}
} catch (RecordStoreNotOpenException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidRecordIDException e) {
} catch (RecordStoreException e) {
}
}
public boolean deleteRS(String rsname) {
if (rsname.length() > 32)
return false;
try {
RecordStore.deleteRecordStore(rsname);
} catch (Exception e) {
return false;
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -