📄 searchexample.java
字号:
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class SearchExample extends MIDlet implements CommandListener
{
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command start;
//声明记录存储对象
private RecordStore recordstore = null;
//声明记录存储枚举器对象
private RecordEnumeration recordEnumeration = null;
//声明记录存储过滤器对象
private Filter filter = null;
public SearchExample ()
{
display = Display.getDisplay(this);
exit = new Command("Exit", Command.SCREEN, 1);
start = new Command("Start", Command.SCREEN, 1);
form = new Form("Mixed RecordEnumeration", null);
form.addCommand(exit);
form.addCommand(start);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
}
public void pauseApp()
{
}
public void destroyApp( boolean unconditional )
{
}
public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(true);
notifyDestroyed();
}
else if (command == start)
{
try
{
//创建或者打开记录存储
recordstore = RecordStore.openRecordStore(
"myRecordStore", true );
}
catch (Exception error)
{
alert = new Alert("Error Creating",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
//声明向记录存储中插入的记录的数据
String outputData[] = {"Mary", "Bob", "Adam"};
//向记录存储中添加记录
for (int x = 0 ; x < 3; x++)
{
byte[] byteOutputData = outputData[x].getBytes();
recordstore.addRecord(byteOutputData, 0,
byteOutputData.length);
}
}
catch ( Exception error)
{
alert = new Alert("Error Writing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
//实例化过滤器
filter = new Filter("Bob");
//使用过滤器作参数实例化枚举器
recordEnumeration = recordstore.enumerateRecords(
filter, null, false);
//将符合过滤条件的记录显示在屏幕上
if (recordEnumeration.numRecords() > 0)
{
String string = new String(recordEnumeration.nextRecord());
alert = new Alert("Reading", string,
null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
catch (Exception error)
{
alert = new Alert("Error Reading",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
try
{
//关闭记录存储
recordstore.closeRecordStore();
}
catch (Exception error)
{
alert = new Alert("Error Closing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
//判断是否还有对记录存储的打开引用
if (RecordStore.listRecordStores() != null)
{
try
{
//删除记录存储
RecordStore.deleteRecordStore("myRecordStore");
//销毁枚举器对象
recordEnumeration.destroy();
//关闭过滤器
filter.filterClose();
}
catch (Exception error)
{
alert = new Alert("Error Removing",
error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
}
}
//声明过滤器类
class Filter implements RecordFilter
{
//过滤标准
private String search = null;
private ByteArrayInputStream inputstream = null;
private DataInputStream datainputstream = null;
//构造函数
public Filter(String search)
{
//通过构造函数初始化过滤标准
this.search = search.toLowerCase();
}
//实现过滤器
public boolean matches(byte[] suspect)
{
//如果符合标准则返回true,不符合则返回false
String string = new String(suspect).toLowerCase();
if (string!= null && string.indexOf(search) != -1)
return true;
else
return false;
}
public void filterClose()
{
try
{
if (inputstream != null)
{
inputstream.close();
}
if (datainputstream != null)
{
datainputstream.close();
}
}
catch ( Exception error)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -