Use the record storage system (RMS) to store information in j2s

Source: Internet
Author: User

Use the record storage system (RMS) to store information in j2s

 

For more articles visit: http://blog.csdn.net/mailbomb

 

In MIDP, there is no file concept, so permanent storage can only be implemented by the record storage system. For details about the record storage system, refer to the Tutorial:

Http://www-900.ibm.com/developerWorks/cn/java/j-wi-rms/index.shtml

Below are some common encoding descriptions for record storage systems:

1. Open record set:

Open the record set and use the static method openrecordstore in recordstore. The sample code is as follows:

Recordstore rs = recordstore. openrecordstore ("username", true );

In this way, a record set named RS is opened, where username is the name of the record set, which can be obtained as needed, the second parameter indicates that a new record set is created otherwise, and true indicates that a new record set is created when the record set does not exist. False indicates that the record set is not created.

If the record set does not exist when the record set is opened, a recordstorenotfoundexception exception is thrown. Therefore, you can check whether the record set has been created and can use this exception.

Note: Close the record set after it is enabled.

2. Write Data to the record set

2.1 increase data

To add data to an opened record set, you must use the addrecord method. Example code:

Byte [] bytes = {1, 2, 3 };

Int id = Rs. addrecord (bytes, 0, bytes. Length );

This code writes all the content of bytes in the byte array to the record set. The return value of this method is the ID of this information. Note: Id starts from 1, not from 0.

You can use this method repeatedly to write multiple data records to the record set.

2.2 modify data

To modify the data of the specified ID in an existing record set, use the setrecord method. Example code:

Byte [] bytes = {1, 2, 3 };

Rs. setrecord (1, bytes, 0, bytes. Length );

The above code is used to write all the bytes in the byte array to the record set RS with ID 1.

This operation overwrites existing data.

Note: When you need to write information to the first record in the record set, you can combine the above two methods to add data to the record set for the first time and modify it later.

3. read data from the record set

To read existing data from the record set, use the getrecord method. Example code:

Byte [] bytes = Rs. getrecord (1 );

This Code reads the first data from the record set RS and places the read data in the bytes array.

When reading data, you can obtain the number of IDs in the record set, which can be obtained using the getnumrecords method.

The Code is as follows:

Int number = Rs. getnumrecords ();

Int id = 1;

If (ID> 0 & id <number ){

Byte [] bytes = Rs. getrecord (1 );

}

4. delete records from the record set

There are two methods to delete a record from a record set: Logical deletion and physical deletion.

Logical deletion refers to marking the deleted records.

Physical deletion refers to the physical deletion of the record, but the record ID cannot be reused, that is, the ID will not be used again. For example, there are five records in a record set. If you delete the data whose ID is 3, the IDs of the remaining records are still 1, 2, 4, and 5. This brings us some trouble.

5. Calendar record set

The calendar record set, that is, all data in the access record set, has two methods. For details, see:

Http://gceclub.sun.com.cn/NASApp/sme/controller/teclist? Tid = 0103

 

6. Other operations

6.1 delete a record set

The deleterecordstore method must be used to delete a record set. Example code:

Recordstore. deleterecordstore ("username ");

This code deletes a record set named username.

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.