大家在做J2EE开发时数据存储一般是放在数据库中进行,在开发手机应用时用J2ME技术存储数据的方式便会用到RMS,RMS具体细节在网上GOOGLE下.这里直接贴代码.

  

建立一个包含以下字段的表

int index;
 String name;
 String fileUrl;
 int picSize;
 int textSize;
 String other;

第一步:TextResBean 类封装我用到的字段数据

public class TextResBean {
  private int index;
  private String name;
  private String fileUrl;
  private int picSize;
  private int textSize;
  private String other;public int getIndex() {
   return index;
  }public void setIndex(int index) {
   this.index = index;
  }public String getName() {
   return name;
  }public void setName(String name) {
   this.name = name;
  }public String getFileUrl() {
   return fileUrl;
  }public void setFileUrl(String fileUrl) {
   this.fileUrl = fileUrl;
  }public int getPicSize() {
   return picSize;
  }public void setPicSize(int picSize) {
   this.picSize = picSize;
  }public int getTextSize() {
   return textSize;
  }public void setTextSize(int textSize) {
   this.textSize = textSize;
  }public String getOther() {
   return other;
  }public void setOther(String other) {
   this.other = other;
  }
  public TextResBean() {}
public TextResBean(int index, String name, String fileUrl, int picSize,
    int textSize, String other) {
   super();
   this.index = index;
   this.name = name;
   this.fileUrl = fileUrl;
   this.picSize = picSize;
   this.textSize = textSize;
   this.other = other;
  }  public TextResBean(byte[] rec) {
   initPlayListBean(rec); // 读取RMS内容的构造函数
  }
  public byte[] toBytes() { // 写成字节 byte[] data = null;
 try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeInt(index);
    dos.writeUTF(name);
    dos.writeUTF(fileUrl);
    dos.writeInt(picSize);
    dos.writeInt(textSize);
    dos.writeUTF(other);
    data = baos.toByteArray();
    baos.close();
    dos.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
   return data;
  }
  public void initPlayListBean(byte[] rec) { // 从字节读取内容 ByteArrayInputStream bais = new ByteArrayInputStream(rec);
   DataInputStream dis = new DataInputStream(bais);
   try {
    index = dis.readInt();
    name = dis.readUTF();
    fileUrl = dis.readUTF();
    picSize = dis.readInt();
    textSize = dis.readInt();
    other = dis.readUTF();
   } catch (Exception e) {
    e.printStackTrace();
   }
  }
 }第二步:TextMusicResRMS类RMS操作封装
public class TextMusicResRMS {
  private static TextMusicResRMS PLRMS = new TextMusicResRMS();
  private RecordStore rs;public static TextMusicResRMS getInstance() {
   if (PLRMS == null) {
    PLRMS = new TextMusicResRMS();
   }
   return PLRMS;
  }  private TextMusicResRMS() {
}
public void openRS(String rsname) {
   try {
    rs = RecordStore.openRecordStore(rsname, true);
   } catch (Exception e) {
    System.out.println("打开记录异常");
   }
  }  public void closeRS() {
   try {
    rs.closeRecordStore();
   } catch (Exception e) {
    System.out.println("关闭记录异常");
   }
  }  public boolean addRecord(String rsname, String name, String fileUrl,
    int picSize, int textSize, String other) {
   boolean success = false;
   openRS(rsname);
   try {
    int index = rs.getNumRecords() + 1;
    TextResBean plb = new TextResBean(index, name, fileUrl, picSize,
      textSize, other);
    byte[] data = plb.toBytes();
    rs.addRecord(data, 0, data.length);
    success = true;
   } catch (Exception e) {
    e.printStackTrace();
    System.out.println("添加记录异常");
   }
   closeRS();
   return success;
  } 
  public int getNumOfRecords() {// 得到RMS中记录的条数
   try {
    int x = rs.getNumRecords();
    return x;
   } catch (Exception e) {
    System.out.println("获取记录总数异常");
    return 0;
   }
  }  public TextResBean[] getRecords(String rsname) {// 取得RMS中的所有记录
   openRS(rsname);
   TextResBean[] result = {}; try {
    rs.enumerateRecords(null, null, false);
    result = new TextResBean[rs.getNumRecords()];
    for (int i = 0; i < result.length; i++) {
     TextResBean plb = new TextResBean(rs.getRecord(getId(i + 1)));   result[i] = plb;
    }
   } catch (Exception e) {
    System.out.println("取得所有记录异常" + e.getMessage());
   }
   closeRS();
   return result;
  }  public TextResBean getRecord(int j) {// 根据记录编号(参数 int j)取得一条记录
   TextResBean result = new TextResBean();
   try {
    rs.enumerateRecords(null, null, false);
    result = new TextResBean(rs.getRecord(getId(j)));
   } catch (Exception e) {
    e.printStackTrace();
    System.out.println("根据编号取记录异常" + e.getMessage());
   }
   return result;
  }  public int getId(int index) {// 得到记录号int j
   RecordEnumeration re = null;
   int x = 1;
   try {
    re = rs.enumerateRecords(null, null, false); // enumeration
    int sum = getNumOfRecords();
    for (int i = 0; i < sum; i++) {
     int j = re.nextRecordId();
     TextResBean plb = new TextResBean(rs.getRecord(j));
     if (plb.getIndex() == index) {
      x = j;
     }
    }
   } catch (Exception e) {
    System.out.println("得到记录号异常");
   }
   return x;
  }public boolean setRecord(int id, int index, String name, String fileUrl,
    int picSize, int textSize, String other) {
   boolean success = false;
   try {
    rs.enumerateRecords(null, null, false);
    TextResBean plb = new TextResBean(index, name, fileUrl, picSize,
      textSize, other);
    byte[] data = plb.toBytes();
    rs.setRecord(id, data, 0, data.length);
    success = true;
   } catch (Exception e) {
    System.out.println("修改记录异常");
    e.printStackTrace();
   }
   return success;
  }
  public void deleteRecord(int id) { try {
    rs.deleteRecord(id);
   } catch (Exception e) {
    System.out.println("删除记录异常");
    e.printStackTrace();
   }}
  public void deletePlAllRecord(String rsname) {
   openRS(rsname);
   try {
    int reccount = rs.getNumRecords();
    for (int i = 1; i <= reccount; i++) {
     deleteRecord(i);
    }
   } catch (Exception e) {
    System.out.println("删除记录异常");
    e.printStackTrace();
   }
   closeRS();
  }  public void deletePlRecordStore(String rsname) {
   // openRS(rsname);
   try {
    RecordStore.deleteRecordStore(rsname);
   } catch (Exception e) {
    System.out.println("清空异常");
   }
   // closeRS();
  }  public void deleteRecord(String rsname, int index) {
   openRS(rsname);
   int SourceRecordId = getId(index);
   deleteRecord(SourceRecordId);
   int sum = getNumOfRecords();
   for (int i = index; i <= sum; i++) {
    int sRecordId = getId(i + 1);
    TextResBean plb = getRecord(i + 1);
    setRecord(sRecordId, i, plb.getName(), plb.getFileUrl(), plb
      .getPicSize(), plb.getTextSize(), plb.getOther());
   }
   closeRS();
  }}
第三步 如何调用
TextMusicResRMS resrms = TextMusicResRMS.getInstance();
resrms.addRecord(String rsname, String name, String fileUrl,
    int picSize, int textSize, String other);resrms.deleteRecord(String rsname, int index) {
 resrms.getRecords(resname);