import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
public class compareRecord extends MIDlet implements RecordComparator{
RecordStore rs=null;
public compareRecord() {
// TODO Auto-generated constructor stub
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
try {
rs=RecordStore.openRecordStore("rs", true);
rs.addRecord("443".getBytes(), 0, "443".getBytes().length);
rs.addRecord("34".getBytes(), 0, "34".getBytes().length);
rs.addRecord("344".getBytes(), 0, "344".getBytes().length);
rs.addRecord("23".getBytes(), 0, "23".getBytes().length);
rs.addRecord("354".getBytes(), 0, "354".getBytes().length);
//方法一,枚举遍历
RecordEnumeration re= rs.enumerateRecords(null, this, false); //能发现失效的ID并自动跳过
while(re.hasNextElement())
{
System.out.println(new String(re.nextRecord()));
}
rs.closeRecordStore();
} catch (RecordStoreFullException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RecordStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int compare(byte[] arg0, byte[] arg1) {
// TODO Auto-generated method stub
int num1=Integer.parseInt(new String(arg0));
int num2=Integer.parseInt(new String(arg1));
if(num1<num2)
return RecordComparator.FOLLOWS; //arg0放到arg1的后面
else if(num2<num1)
return RecordComparator.PRECEDES; //arg0放到arg1的前面
return RecordComparator.EQUIVALENT; //arg0和arg1无分先后
}
}
本文介绍了一个使用Java ME实现的记录存储应用案例。该案例通过创建一个实现了RecordComparator接口的类compareRecord,演示了如何对存储在RecordStore中的记录进行排序和比较。文中详细展示了如何打开记录存储、添加记录以及通过实现RecordComparator接口的方法来定义记录间的比较规则。
162

被折叠的 条评论
为什么被折叠?



