📄 users.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.util.*;
public class UseRS extends MIDlet implements CommandListener{
private Command exitCommand, createCmd, delCmd, useCmd;
private Display display;
private RecordStore rs; // 记录存储rs在打开、关闭操作中使用
private TextBox showMsg; // 用于显示操作结果信息
private String msg; // 存放用于文本框显示的信息
public UseRS() {
display = Display.getDisplay(this);
showMsg = new TextBox("操作信息:", null, 200, TextField.ANY);
exitCommand =new Command("退出",Command.EXIT,1);
createCmd =new Command("建立",Command.SCREEN,1);
useCmd =new Command("使用",Command.SCREEN,1);
delCmd =new Command("删除",Command.SCREEN,1);
showMsg.addCommand(exitCommand);
showMsg.addCommand(createCmd);
showMsg.addCommand(useCmd);
showMsg.addCommand(delCmd);
showMsg.setCommandListener(this);
}
public void startApp(){
// 显示当前MIDlet套件中已经建立的记录存储名称
msg = "套件中已经建立的记录存储有: ";
String [] RSExist = RecordStore.listRecordStores();
if (RSExist != null) {
for (int i=0; i<RSExist.length; i++) {
msg += "\n" + RSExist[i];
}
}
showMsg.setString(msg); // 更新文本框中显示的内容
display.setCurrent(showMsg);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c,Displayable d) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else if (c == createCmd) {
if (ExistRS("myRS"))
msg = "记录存储myRS己经存在";
else if (CreateRS("myRS"))
msg = "记录存储myRS创建成功";
else
msg = "记录存储myRS创建失败";
}
else if(c == delCmd) {
if (!ExistRS("myRS"))
msg = "记录存储myRS不存在";
else if (DeleteRS("myRS"))
msg = "记录存储myRS删除成功";
else
msg = "记录存储myRS删除失败";
}
else if (c == useCmd) {
if (!ExistRS("myRS"))
msg = "记录存储myRS不存在";
else {
if (OpenRS("myRS")) {
msg = "记录存储myRS打开成功";
// 获取打开的记录存储的信息
try {
Date lmt = new Date(rs.getLastModified());
msg += "\nLastModified = " + lmt.toString();
msg += "\nName = " + rs.getName();
msg += "\nVersion = " + rs.getVersion();
msg += "\nSize = "+ rs.getSize();
msg += "\nSizeAvailable = " + rs.getSizeAvailable();
}
catch (RecordStoreException e) { }
}
else
msg = "记录存储myRS打开失败";
if (CloseRS())
msg += "\n记录存储myRS关闭成功";
else
msg += "\n记录存储myRS关闭失败";
}
}
showMsg.setString(msg);
}
// 检测当前套件中记录存储是否存在
public boolean ExistRS(String rsName) {
try {
RecordStore r = RecordStore.openRecordStore(rsName, false);
r.closeRecordStore();
r = null;
}
// 捕捉到记录存储找不到的异常,表示记录存储不存在
catch(RecordStoreNotFoundException e1) {
return false;
}
catch(RecordStoreException e2) { }
return true;
}
// 创建记录存储,之前需先检查记录存储是否已经存在
public boolean CreateRS(String rsName) {
// 创建一个记录存储
try {
RecordStore r = RecordStore.openRecordStore(rsName, true);
r.closeRecordStore();
r=null;
return true;
}
catch(RecordStoreException e) { }
return false;
}
// 删除记录存储,之前需先检查记录存储是否已经存在
public boolean DeleteRS(String rsName) {
try {
RecordStore.deleteRecordStore(rsName);
return true;
}
catch(RecordStoreException e) { }
return false;
}
// 打开记录存储
public boolean OpenRS(String rsName) {
try {
rs = RecordStore.openRecordStore(rsName, false);
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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -