📄 listenrs.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class ListenRS extends MIDlet implements CommandListener{
private Command exitCmd, addCmd, delCmd, setCmd, getCmd;
private Display display;
private RecordStore rs; // 定义记录存储rs
private TextBox showMsg; // 用于显示操作结果信息
private String msg = ""; // 存放用于文本框显示的信息
public ListenRS() {
display = Display.getDisplay(this);
showMsg = new TextBox("操作信息:", null, 512, TextField.ANY);
exitCmd =new Command("退出",Command.EXIT,1);
addCmd =new Command("添加记录",Command.SCREEN,1);
delCmd =new Command("删除记录",Command.SCREEN,1);
setCmd =new Command("修改记录",Command.SCREEN,1);
getCmd =new Command("浏览记录",Command.SCREEN,1);
showMsg.addCommand(exitCmd);
showMsg.addCommand(addCmd);
showMsg.addCommand(delCmd);
showMsg.addCommand(setCmd);
showMsg.addCommand(getCmd);
showMsg.setCommandListener(this);
}
public void startApp(){
if (!OpenRS("tempRS"))
msg = "记录存储tempRS打开失败";
else
msg = "记录存储tempRS打开成功";
showMsg.setString(msg);
display.setCurrent(showMsg);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) {
if (CloseRS())
System.out.println("记录存储tempRS关闭成功");
else
System.out.println("记录存储tempRS关闭失败");
if (DeleteRS("tempRS"))
System.out.println("记录存储tempRS删除成功");
else
System.out.println("记录存储tempRS删除失败");
}
public void commandAction(Command c,Displayable d) {
int recId;
if (c == exitCmd) {
destroyApp(false);
notifyDestroyed();
}
else if (c == addCmd) {
if ((recId = AddRec()) != -1)
msg = "记录添加成功,记录号为 " + recId;
else
msg = "记录添加失败";
showMsg.setString(msg);
}
else if (c == delCmd) {
if ((recId = DelRec()) != -1)
msg = "记录删除成功,记录号为 " + recId;
else
msg = "记录删除失败";
showMsg.setString(msg);
}
else if (c == setCmd) {
if ((recId = SetRec()) != -1)
msg = "记录修改成功,记录号为 " + recId;
else
msg = "记录修改失败";
showMsg.setString(msg);
}
else if (c == getCmd) {
msg = GetRec();
showMsg.setString(msg);
}
}
// 增加记录,返回所增加记录的记录号,返回-1表示出错
public int AddRec() {
int recID = -1;
try {
int id = rs.getNextRecordID();
String rec = "Data with RecordID " + id;
byte [] data = rec.getBytes();
recID = rs.addRecord(data, 0, data.length);
}
catch (RecordStoreException e) { }
return recID ;
}
// 删除记录,返回所删除记录的记录号,返回-1表示出错
public int DelRec() {
try {
RecordEnumeration re = rs.enumerateRecords(null, null, false);
if (re.hasNextElement()) {
int id = re.nextRecordId();
rs.deleteRecord(id);
return id;
}
}
catch (RecordStoreException e) { }
return -1;
}
// 修改记录,返回所修改记录的记录号,返回-1表示出错
public int SetRec() {
try {
RecordEnumeration re = rs.enumerateRecords(null, null, false);
if (re.hasNextElement()) {
int id = re.nextRecordId();
String rec = "Modified Data with RecordID " + id;
byte [] data = rec.getBytes();
rs.setRecord(id, data, 0, data.length);
return id;
}
}
catch (RecordStoreException e) { }
return -1;
}
// 浏览记录存储中所有记录
public String GetRec() {
String message = null;
try {
RecordEnumeration re = rs.enumerateRecords(null, null, false);
int num = re.numRecords();
message = "记录存储中共有" + num + "条记录:";
byte [] data = null;
int count = 0;
while(re.hasNextElement()){
count ++;
data = re.nextRecord();
message += "\n第 " + count + " 条记录:";
message += "\n" + new String(data);
}
}
catch (RecordStoreException e) {
message = "记录读取失败!";
}
return message;
}
// 打开(创建)记录存储
public boolean OpenRS(String rsName) {
try {
rs = RecordStore.openRecordStore(rsName, true);
// 注册记录监听器
rs.addRecordListener(new MyRecordListener());
return true;
}
catch(RecordStoreException e) { }
return false;
}
// 关闭记录存储
public boolean CloseRS() {
if(rs != null) {
try {
rs.closeRecordStore();
rs = null;
return true;
}
catch(RecordStoreException e) { }
}
return false;
}
// 删除记录存储
public boolean DeleteRS(String rsName) {
try {
RecordStore.deleteRecordStore(rsName);
return true;
}
catch(RecordStoreException e) { }
return false;
}
}
// 定义监听器,监听记录的添加、删除和修改事件
class MyRecordListener implements RecordListener {
// 响应记录添加事件
public void recordAdded(RecordStore recordStore,int recordID){
try{
String rsName = recordStore.getName();
System.out.println("在记录存储" + rsName +
"中添加记录,记录号为"+recordID);
}
catch(RecordStoreException e){ }
}
// 响应记录修改事件
public void recordChanged(RecordStore recordStore,int recordID){
try{
String rsName = recordStore.getName();
System.out.println("在记录存储" + rsName +
"中修改记录,记录号为"+recordID);
}
catch(RecordStoreException e){ }
}
// 响应记录删除事件
public void recordDeleted(RecordStore recordStore,int recordID){
try{
String rsName = recordStore.getName();
System.out.println("在记录存储" + rsName +
"中删除记录,记录号为"+recordID);
}
catch(RecordStoreException e){ }
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -