- The javax.microedition.rms Package
- Accessing Record Stores with the RecordStore Class
- Building an Address Book for J2ME Devices
- Conclusion
- Additional Resources
Accessing Record Stores with the RecordStore Class
The RecordStore class is the developer's interface to the underlying RMS. Record stores are created in a platform-dependent manner by the midlet environment. Actual locations and storage information are hidden from the application developer. Instead, record stores can be accessed using a simple naming convention: Names can be up to 32 Unicode characters long, are case sensitive, and must be unique within a midlet suite (a collection of midlets and resources bundled together in a compressed JAR file). All midlets within a midlet suite have read/write access to a record store, assuming they know the correct name. Should that midlet suite be removed from the device, all record stores associated with that suite will also be deleted.
To open a record store named TestRecordSet, for example, you call the RecordStore.openRecordStore() method. This method takes two arguments: a string indicating the record store name, and a Boolean that—if true—will create the record store if it doesn't already exist. To create our new TestRecordSet record store, we make the following method calls:
RecordStore rs = null; rs = RecordStore.openRecordStore("TestRecordSet", true);
After this record store has been created, we can add data to the record store by calling the RecordStore.addRecord() method. addRecord() accepts three arguments:
Argument |
Description |
byte[] data |
An array of byte data to be stored in this record. Typically, data is added to the byte array via the java.io.ByteArrayOutputStream and java.io.DataOutputStream classes. |
int offset |
The index into the data buffer of the first relevant byte of this record. |
int numBytes |
The number of bytes of the data buffer to use for this record. |
On successful completion of the addRecord() call, the method returns an integer specifying the ID of the record in the record store. Although I won't use them in my examples in this article, RecordStore also specifies other data editing methods, including setRecord() and deleteRecord().