In J2ME, the importance of RMS as the only permanent storage tool is self-evident. But a lot of new recruits who just started to learn J2ME always complain that there is little or no pertinence to the information. Therefore, I would like to put myself in this aspect of some learning experience and you exchange.
RMS, the record Manager System, is often used as a tool for scoring records, gaming information storage, and so on in mobile applications.
The use of RMS can be divided into two parts: one, the construction of a single record, and the use and operation of RecordStore. These two aspects are described in detail below.
One, the construction of a single record. We may need to record a lot of similar entries when we store records, where we can think of this structure as a database, where we're going to construct one row in the database, the construction of a single record. The source code of the program is as follows:
package com.cuilichen.usual;
Import java.io.bytearrayinputstream;//The various input and output streams to be used
import Java.io.ByteArrayOutputStream;
import Java.io.DataInputStream;
import Java.io.DataOutputStream;
public class Appointment {//single record class name
private int int1; //
private int int2; //
Private Long long1;
private String str1; STR1 as a reserved field, records the keyword of the search
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; Constructor
written to 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); Read the constructor for the RMS content
}
public byte[] Tobytes () {//write byte
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) {//reads content from bytes
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 write flag
return wroteflag;
}
}
The use of this class ensures that the content is written and output when we use the stream. Of course, as with the design of database tables, we can add or decrease fields for every record, and in the class above I only use INT1,INT2,LONG1,STR1,STR2,STR3 and Wroteflag altogether 7 fields.