📄 recordsort.java
字号:
import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
public class RecordSort extends MIDlet{
private RecordStore rs = null;
static final String REC_STORE = "db_1";
public RecordSort(){
openRecStore(); // Create the Record Store
// Write a few records
writeRecord("Chinese");
writeRecord("SiChuan");
writeRecord("ChengDu");
writeRecord("BeiJing");
// Read back with enumerator, sorting the results
readRecords();
closeRecStore(); // Close Record Store
deleteRecStore(); // Remove the Record Store
}
public void destroyApp( boolean unconditional ){}
public void startApp(){
// There is no user interface, go ahead and shutdown
destroyApp(false);
notifyDestroyed();
}
public void pauseApp(){}
public void openRecStore(){
try{
// Create Record Store if it does not exist
rs = RecordStore.openRecordStore(REC_STORE, true );
}catch (Exception e){
db(e.toString());
}
}
public void closeRecStore(){
try{
rs.closeRecordStore();
}catch (Exception e){
db(e.toString());
}
}
public void deleteRecStore(){
if (RecordStore.listRecordStores() != null){
try{
RecordStore.deleteRecordStore(REC_STORE);
}catch (Exception e){
db(e.toString());
}
}
}
public void writeRecord(String str){
byte[] rec = str.getBytes();
try{
rs.addRecord(rec, 0, rec.length);
}catch (Exception e){
db(e.toString());
}
}
public void readRecords(){
try{
if (rs.getNumRecords() > 0){
Comparator comp = new Comparator();
RecordEnumeration re = rs.enumerateRecords(null, comp, false);
while (re.hasNextElement()){
String str = new String(re.nextRecord());
System.out.println(str);
System.out.println("------------------------------");
}
}
}
catch (Exception e){
db(e.toString());
}
}
/*--------------------------------------------------
* Simple message to console for debug/errors
*-------------------------------------------------*/
private void db(String str){
System.err.println("Msg: " + str);
}
}
/*--------------------------------------------------
| Comparator.java
|
| Compares two records to determine sort order
*-------------------------------------------------*/
class Comparator implements RecordComparator{
public int compare(byte[] rec1, byte[] rec2){
String str1 = new String(rec1), str2 = new String(rec2);
int result = str1.compareTo(str2);
if (result == 0)
return RecordComparator.EQUIVALENT;
else if (result < 0)
return RecordComparator.PRECEDES;
else
return RecordComparator.FOLLOWS;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -