在J2ME中,RMS作為唯一的永久性存儲工具,其重要性是不言而喻的。但是很多剛剛開始學習J2ME的新人總是抱怨在這方面的資料很少,或者是針對性不強。因此,我想把自己在這方面的一些學習心得和大家交流一下。 RMS即Record Manager System,在手機應用中常常作為得分記錄、遊戲信息存儲等的工具使用。 RMS的使用可以分為兩個部分:一、單一記錄的構造;二、RecordStore的使用和操作。下面就這兩方面進行詳細說明。 一、單一記錄的構造。我們在存儲記錄時可能需要記錄很多相似的條目,在這裡我們可以把這種結構看成資料庫,我們在這一步就是要構造資料庫中的一行,即單一記錄的構造。程序的源碼如下: package com.cuilichen.usual; import java.io.ByteArrayInputStream;//要使用到的各種輸入輸出流 import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; public class Appointment {//單一記錄的類名 private int int1; // private int int2; // private long long1; private String str1; //str1作為保留欄位,記錄檢索的關鍵字 private String str2; // private String str3; // private boolean WroteFlag; // public Appointment() { } public Appointment(int _int1, int _int2, long _long1, String _str1, String _str2, String _str3, boolean _WroteFlag) { this.int1 = _int1; //寫入RMS的構造函數 this.int2 = _int2; this.long1 = _long1; this.str1 = _str1; this.str2 = _str2; this.str3 = _str3; this.WroteFlag = _WroteFlag; } public Appointment(byte[] rec) { initAppointmnet(rec); //讀取RMS內容的構造函數 } public byte[] toBytes() { //寫成位元組 byte[] data = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(int1); dos.writeInt(int2); dos.writeLong(long1); dos.writeUTF(str1); dos.writeUTF(str2); dos.writeUTF(str3); dos.writeBoolean(WroteFlag); data = baos.toByteArray(); baos.close(); dos.close(); } catch (Exception e) { e.printStackTrace(); } return data; } public void initAppointmnet(byte[] rec) { //從位元組讀取內容 ByteArrayInputStream bais = new ByteArrayInputStream(rec); DataInputStream dis = new DataInputStream(bais); try { int1 = dis.readInt(); int2 = dis.readInt(); long1 = dis.readLong(); str1 = dis.readUTF(); str2 = dis.readUTF(); str3 = dis.readUTF(); WroteFlag = dis.readBoolean(); } catch (Exception e) { e.printStackTrace(); } } public int getInt1() { //int return int1; } public int getInt2() { return int2; } public long getLong1() { return long1; } public String getStr1() { //String return str1; } public String getStr2() { //String return str2; } public String getStr3() { return str3; } public boolean getWroteFlag() { //返回寫入標誌 return WroteFlag; } } 這個類的使用保證了我們在使用流時,內容的寫入和輸出。當然,就如同資料庫表的設計一樣,我們可以任意對每一條記錄增加或減少欄位,在上面的類中我只使用了int1,int2,long1,str1,str2,str3和WroteFlag一共7個欄位。 二、RecordStore的操作。類RMS如下: package com.cuilichen.usual; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; public class RMS { public static final int Int1 = 0;//各個欄位的默認數值 public static final int Int2 = 0; public static final long Long1 = 0; public static final String Str1 = ""; public static final String Str2 = ""; public static final String Str3 = ""; public static boolean addRecord(String name, int int1, int int2,//添加記錄 long long1, String str1, String str2, String str3, boolean b) { boolean success = false; try { RecordStore rs = RecordStore.openRecordStore(name, true); Appointment app = new Appointment(int1, int2, long1, str1, str2,str3, b); //既然str1作為保留欄位,我們在這裡就要如此操作:例如int1為我們設定的關鍵字,那麼str1 = Integer.toString(int1); byte[] data = app.toBytes(); rs.addRecord(data, 0, data.length); rs.closeRecordStore(); success = true; } catch (Exception e) { e.printStackTrace(); } return success; } public static int getNumOfRecords(String name) {//得到RMS中記錄的條數 try { RecordStore rs = RecordStore.openRecordStore(name, true); return rs.getNumRecords(); } catch (Exception e) { return 0; } } public static Appointment[] getRecords(String name) {//取得RMS中的所有記錄 Appointment[] result = { }; try { RecordStore rs = RecordStore.openRecordStore(name, false); RecordEnumeration re = rs.enumerateRecords(null, null, false); result = new Appointment[rs.getNumRecords()]; for (int i = 0; i < result.length; i++) { int j = re.previousRecordId(); Appointment app = new Appointment(rs.getRecord(j)); result = app; //System.out.println("app["+i+"] "+app.getStr2()); } rs.closeRecordStore(); } catch (Exception e) { } return result; } public static Appointment getRecord(String name, int j) {//根據記錄編號(參數 int j)取得一條記錄 Appointment result = new Appointment(); try { RecordStore rs = RecordStore.openRecordStore(name, false); RecordEnumeration re = rs.enumerateRecords(null, null, false); result = new Appointment(rs.getRecord(j)); rs.closeRecordStore(); } catch (Exception e) { } return result; } public static int getIndex(String name, String content) {//得到記錄號int j,這裡需要使用保留欄位str1 RecordStore rs = null; RecordEnumeration re = null; try { rs = RecordStore.openRecordStore(name, false); //open re = rs.enumerateRecords(null, null, false); //enumeration for (int i = 0; i < RMS.getNumOfRecords(name); i++) { int j = re.nextRecordId(); Appointment app = new Appointment(rs.getRecord(j)); if (app.getStr1().equals(content)) { return j; } } } catch (Exception e) { } return 1; } public static boolean setRecord(String name, int id, int int1, int int2,//設置記錄號為id的記錄 long long1, String str1, String str2, String str3, boolean b) { boolean success = false; RecordStore rs = null; RecordEnumeration re = null; try { rs = RecordStore.openRecordStore(name, false); //open re = rs.enumerateRecords(null, null, false); //enumeration Appointment app = new Appointment(int1, int2, long1, str1, str2, str3, b); //str1作為保留欄位,在這裡如此操作:例如若int1為我們設定的關鍵字,那麼str1 = Integer.toString(int1); byte[] data = app.toBytes(); rs.setRecord(id, data, 0, data.length); success = true; rs.closeRecordStore(); } catch (Exception e) { } return success; } } 在這個類中,我沒有將各個Exception向外拋出,一般來說這樣作是不合適的,它違背了Java的異常處理機制。但是在我使用這個類的各個J2ME程序中,它是可以勝任的,所以也就沒有進行進一步的修改。 有了以上的兩個類和你對RMS的理解,在程序中,你就可以順暢的使用RMS了。 比如在MIDlet開始時,如下操作(增加記錄): protected void startApp() throws MIDletStateChangeException { if (RMS.getNumOfRecords(rsName) = = 0) {//rsName在前面已經聲明了。String rsName=「MyRMS」; for (int i = 0; i <6; i++) { RMS.addRecord(rsName, RMS.Int1, i, RMS.Long1, Integer . toString(i), RMS.Str2, "1234567890123456789",false); } }它就在RMS中增加了6條記錄,其中int1,long1,str2,WroteFlag都沒有使用,我們只是使用int2,str1(作為保留欄位)和str3。 }
[火星人
]
核心技術:J2ME中RMS的使用解析 已經有598 次圍觀
本文地址: http://coctec.com/docs/linux/show-post-189381.html