📄 notebook.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class notebook extends MIDlet implements CommandListener{
private Display display;
private Form form;
private Command cmdExit,cmdOk,cmdQuery;
private TextField textfield;
private RecordStore rs=null;
static final String rsName="NOTEBOOK";
private byte[] data;
int lastID;
int recordID;
RecordFilter rf;
public notebook(){
display=Display.getDisplay(this);
form=new Form("记事本");
cmdExit=new Command("退出",Command.EXIT,1);
cmdOk=new Command("确定",Command.SCREEN,1);
cmdQuery=new Command("查询",Command.SCREEN,1);
textfield=new TextField("请输入名字:","",20,0);
form.addCommand(cmdExit);
form.addCommand(cmdOk);
form.addCommand(cmdQuery);
form.append(textfield);
form.setCommandListener(this);
try{
rs=RecordStore.openRecordStore(rsName,true);
}catch(RecordStoreException e){
}
}
public void startApp(){
display.setCurrent(form);
}
public void pauseApp(){}
public void destroyApp(boolean u){}
public void commandAction(Command c,Displayable d){
if(c==cmdExit){
try{
rs.closeRecordStore();
}catch(RecordStoreException e){
}
destroyApp(true);
notifyDestroyed();
}
if(c==cmdOk){
data=(textfield.getString()).getBytes();
try{
recordID=rs.addRecord(data,0,data.length);
System.out.println("recordID "+recordID+" is added!Total num is "+rs.getNumRecords());
}catch(RecordStoreException e){
System.out.println("Program error!exit");
}
textfield.setString("");
}
if(c==cmdQuery){
try{
int lastID=rs.getNextRecordID();
for(int i=1;i<lastID;i++){
try{
byte[] data1=rs.getRecord(i);
if(data1==null){
System.out.println("null");
rs.deleteRecord(i);
}else{
System.out.println(new String(data1));
}
}catch(InvalidRecordIDException e){
continue;
}
}
}catch(RecordStoreException e){}
rf=new RF((textfield.getString()).getBytes());
try{
RecordEnumeration enu=rs.enumerateRecords(rf,null,false);
System.out.println("查询结果如下:");
while(enu.hasNextElement()){
byte[] data2=enu.nextRecord();
if(data2==null){System.out.println("null");}else{
System.out.println(new String(data2));}
}
}catch(Exception e){
System.out.println("program error"+e.getMessage());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -