J2ME:rms的两种遍历方法

来源:互联网 发布:美国囧哥淘宝店 编辑:程序博客网 时间:2024/06/16 01:39

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.*;

public class searchrecord extends MIDlet {
   
 RecordStore rs=null;
   
 public searchrecord() {
  // 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("龙".getBytes(), 0, 2);
  rs.addRecord("虎".getBytes(), 0, 2);
  rs.addRecord("狍".getBytes(), 0, 2);
  rs.addRecord("蛇".getBytes(), 0, 2);
  rs.addRecord("老鼠".getBytes(), 0, 4);
  rs.deleteRecord(4);
  
  //方法一,枚举遍历
  RecordEnumeration re= rs.enumerateRecords(null, null, false);  //能发现失效的ID并自动跳过
  while(re.hasNextElement())
  {
   System.out.println(new String(re.nextRecord()));
  }
  
  //方法二,getNextRecordID,遍历ID
  for(int i=1;i<rs.getNextRecordID();i++)
  {
   try{
   System.out.println(new String(rs.getRecord(i)));
   }catch(Exception e)
   {}   //忽略异常
  }
  
  
 } 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();
 }
 }

}

原创粉丝点击