In J2ME, RMS as a unique permanent storage tool, its importance is self-evident. But many newcomers who have just started learning J2ME always complain that there are few information in this area, or is not strong. Therefore, I want to exchange some of my own experience in this area and everyone. RMS is RECORD Manager System, which is often used in mobile applications as tools such as score recording, game information storage, etc. The use of RMS can be divided into two parts: one, a single record structure; Second, RecordStore use and operation. Below will be described in detail below. First, a single record structure. We may need to record a lot of similar entries when storing records, where we can see this structure as a database, and we are in this step to construct a line in the database, ie a single record constructor. The source code of the program is as follows: package com.cuilichen.usual;
Import java.io.byteArrayInputStream; // To use the various input output streams Import Java.io.byteArrayoutputStream; import java.io.DataInputStream; import java.io.Dataoutputstream;
Public class appointment {// single record class name private int rt1; // private int rt2; // private long long1; private string str1; // str1 as a reserved field, record retrieval keyword 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; // write RMS constructor this.long1 = _long1 This.Str1 = _STR1; this.Str2 = _STR2; this.Str3 = _Str3; this.wroteflag = _wroteflag;}
Public Appointment (Byte [] REC) {initAppointMnet (REC); // Read the constructor of RMS content}
Public Byte [] Tobytes () {// Write by Bytestep
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) {// read content from byte
BYTEARRAYINPUTSTREAM bais = new byteArrayinputStream (REC); DataInputStream DIS = New DataInputStream (bais);
Try {INT1 = Dis.readint (); INT2 = dish.readint (); long1 = dish.readlong (); str1 = dishdutf (); str2 = disp.readutf (); str3 = disp.readutf (); Wroteflag = disp.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 returnction str1;}
Public string getStr2 () {// string return str2;}
Public string getStr3 () {return str3;}
Public Boolean getWroteflag () {// Returns the write flag Return wroteflag;}} This class is guaranteed to write and output the contents when we use the stream. Of course, like the design of the database table, we can add or reduce fields to each record, in the above class, I only use INT1, INT2, LONG1, STR1, STR2, STR3, and Wroteflag for a total of 7 fields. Second, the operation of RecordStore. The RMS is as follows: package com.cuilichen.usual;
import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; public class RMS {public static final int Int1 = 0; // default value of each field 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, / / Add Record LONG1, STRING 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); // Since STR1 is used as a reserved field, we are here to do this. , For example, INT1 sets the keyword set for us, then str1 = integer.tostring (int1); byte [] data = app.tobytes (); rs.addrecord (data, 0, data.length); rs.closecordstore () ; "Catch (Exception E) {E.PrintStackTrace ();}
Return Success;
Public Static Int GetNumOfRecords (String Name) {// Get the number of rms recorded in RMS Try {RecordStore Rs = RecordStore.OpenRecordStore (name, true);
Return Rs.getnumRecords ();} catch (exception e) {return 0;}}
Public static appointment [] getRecords (String Name) {// Get all records in 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 rs.closecordStore ();} catch (exception e) {} Return Result;} Public Static Appointment GetRecord (String Name, INT J) {// Number of records based on record numbers (Parameters INT J) (); 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) {// gets the record number Int J, here you need to use the reserved field 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 IF (app.getstr1 (). Equals (content) {Return J;}}} catch (Exception E) {} Return 1;} Public Static Boolean SetRecord (String Name, Int, INT INT1, INT INT2, // Setting record number ID LONG1, STRING STRIN, STRING STR2, STRING STRING STRING STR2, STRING STR3, BOOLEAN B) {Boolean Success = False; RecordStore RS = NULL; RecordenuMeration Re = Null; try {r = RecordStore.OpenRecordStore (Name, false); // Open re = rs.enumerateRecords (null, null, false); // enumeration Appointment App = New Appointment (INT1, INT2, LONG1, STR1, STR2, STR3, B); // STR1 As the reserved field, this is here: for example, if INT1 is a keyword set, Str1 = integer.tostring (INT1); Byte [] Data = app.tobytes (); rs.seTRecord (ID, DATA, 0, DATA.LENGTH); Success = true; rs.closecordStore ();} catch (exception e) {} Return Success;}} In this class, I didn't thrown each Exception, which generally said that this is inappropriate, it violates the Java's exception handling mechanism. But in each J2ME program I use this class, it is competent, so there is no further modification. With the above two classes and your understanding of RMS, in the program, you can use RMS smoothly. For example, when the MIDlet begins (adds a record): protected void startapp () throws midletStateChangeException {if (Rms.GetNumofRecords (RSName) = 0) {// rsName has declared in front. 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);}} It adds 6 records in RMS, where INT1, long1, str2, wroteflag are not used, we just use int2, STR1 (as reserved field) and STR3. } Other operations are similar, which can be implemented in RMS class. Today, I will introduce so much. If I don't understand, I can contact me MSN: cuilichen@hotmail.com This is my first 9CBS blog article, I hope everyone will support it.