Usage resolution of RMS in J2ME

Source: Internet
Author: User
Tags constructor exception handling final integer reserved tostring
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, record 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; Writes to the RMS constructor
This.int2 = _int2;
This.long1 = _long1;
THIS.STR1 = _STR1;
THIS.STR2 = _STR2;
THIS.STR3 = _STR3;
This. Wroteflag = _wroteflag;
}

Public appointment (byte[] rec) {
Initappointmnet (REC); To read the constructor of 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) {//read 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.
Second, the recordstore operation. The class 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 values for individual fields
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 records
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);
Now that STR1 is a reserved field, we're going to do this here: for example, int1 for us, then 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) {//number of bars 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 < result.length; i++) {
Int J = Re.previousrecordid ();
Appointment app = new Appointment (Rs.getrecord (j));
Result[i] = app;

System.out.println ("app[" +i+ "]" +app.getstr2 ());
}

Rs.closerecordstore ();
catch (Exception e) {
}

return result;
}

public static appointment Getrecord (String name, Int j) {//a record is obtained based on the record number (parameter 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) {//Get the record number int J, where you need to use a 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 < 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,//set record number as 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 as a reserved field, where this is done: for example, if int1 for the keyword we set, then str1 = integer.tostring (int1);

byte[] data = App.tobytes ();
Rs.setrecord (ID, data, 0, data.length);
Success = true;
Rs.closerecordstore ();
catch (Exception e) {
}

return success;
}
}
In this class, I did not throw the individual exception out, which is generally inappropriate, and it violates the Java exception handling mechanism. But in the various J2ME programs that I use this class, it is capable, so there is no further modification.
With the above two classes and your understanding of RMS, you'll be able to use RMS smoothly in your program.
For example, at the beginning of MIDlet, the following actions (Add Records):
protected void startApp () throws Midletstatechangeexception {
if (rms.getnumofrecords (rsname) = = 0) {//rsname has been declared earlier. 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 added 6 records to RMS, none of which int1,long1,str2,wroteflag, and we just use INT2,STR1 (as a reserved field) and STR3.
}
Other operations are similar and can be implemented entirely on the RMS class.
So much to introduce today, there is no understanding of the place you can contact me
Msn:cuilichen@hotmail.com
This is my first CSDN blog article, I hope we support a lot.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.