📄 recordenumerationexample.java
字号:
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
public class RecordEnumerationExample extends MIDlet{
private RecordStore recStore = null;
static final String REC_STORE = "test_db";
public RecordEnumerationExample() {
openRecStore(); // Create the Record Store
// Write a few records
writeRecord("CoreJ2ME");
writeRecord("In the middle");
writeRecord("MicroJava");
System.out.println("------------------------------");
readRecords();
System.out.println("------------------------------");
deleteRecord(2);
// This read method will throw an error
// readRecords();
// New and improved read using Enumeration
readRecordsUpdate();
closeRecStore();
}
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{
// The second parameter indicates that the Record Store
// should be created if it does not exist
recStore = RecordStore.openRecordStore(REC_STORE, true );
}
catch (Exception e){
System.err.println(e.toString());
}
}
public void closeRecStore(){
try{
recStore.closeRecordStore();
}
catch (Exception e){
System.err.println(e.toString());
}
}
public void writeRecord(String str){
byte[] rec = str.getBytes();
try{
recStore.addRecord(rec, 0, rec.length);
System.out.println("Writing record: " + str);
}
catch (Exception e){
System.err.println(e.toString());
}
}
public void readRecords(){
try{
// Intentionally small to test code below
byte[] recData = new byte[5];
int len;
for (int i = 1; i <= recStore.getNumRecords(); i++)
{
// Allocate more storage if necessary
if (recStore.getRecordSize(i) > recData.length)
recData = new byte[recStore.getRecordSize(i)];
len = recStore.getRecord(i, recData, 0);
System.out.println("Record ID#" + i + ": " +
new String(recData, 0, len));
}
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public void readRecordsUpdate()
{
try
{
System.out.println("Number of records: " + recStore.getNumRecords());
if (recStore.getNumRecords() > 0)
{
RecordEnumeration re = recStore.enumerateRecords((RecordFilter)null,
(RecordComparator)null, false);
while (re.hasNextElement())
{
String str = new String(re.nextRecord());
System.out.println("Record: " + str);
}
}
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
public void deleteRecord(int id)
{
try
{
recStore.deleteRecord(id);
}
catch (Exception e)
{
System.err.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -