Transfer from: http://blog.9cbs.net/mailbomb
In MIDP, there is no concept of files, so permanent storage can only rely on record storage systems, and you can see the tutorial about recording storage systems:
Http://www-900.ibm.com/developerWorks/cn/java/J-WI-rms/index.shtml
Here are some common coding for recording storage systems:
1, open record set:
Open Record Set Use the static method OpenRecordStore in RecordStore, the sample code is as follows:
RecordStore Rs = RecordStore.OpenRecordStore ("UserName", true);
This opens a recordset named RS, where username is the name of the record set, which can be taken as needed, and the second parameter represents no new record set, TRUE represents the record set When you exist, create a new recordset, False represents not created.
If the recordset does not exist when the recording set is turned on, then the RecordStoreNotFoundException is throwing, so the detection record set has been created to use this exception.
Note: Remember to turn off after the recordset opens.
2, write data to records
2. Add data
Add data to the already opened recordset, you need to use the AddRecord method, sample code:
Byte [] bytes = {1, 2, 3};
INT ID = rs. addrecord (bytes, 0, bytes.length);
This code writes all the contents of the byte array bytes to the record set. The return value of the method is the ID of the information. Note: The ID starts from 1 instead of starting from 0.
You can cycle with this method to write multiple data to the recordset.
2.2 Modifying Data
Modify the data that already exists in the recordset that specifies the ID, you need to use the setRecord method, sample code:
Byte [] bytes = {1, 2, 3};
Rs. setRecord (1, bytes, 0, bytes.length);
The role of the above code is to write all the contents of the byte array BYTES to the recordset RS of the ID 1.
This operation overrides existing data.
Description: Sometimes, you need to write information into the first record in the recordset, you can combine the above two methods, and then add data to the recordset in the first time, in order to modify it.
3, read data from record concentration
Read existing data from the record set, you need to use the getRecord method, sample code:
BYTE [] bytes = rs. getRecord (1);
This code reads the first data from the record set RS and puts the read data in the Bytes array.
When reading the data, you can get the number of IDs in the recordset, you can use the getNumRecords method to get
The integrated code is:
INT Number = rs. getnumRecords ();
INT ID = 1;
IF (id> 0 && id BYTE [] bytes = rs. getRecord (1); } 4. Remove records from records There are two ways to delete records from records: logical deletion and physical deletion. Logic deletion refers to the deleted record tag. Physical deletion refers to the physical deletion of the record, but the ID of the record cannot be reused, that is, the ID will not be used. For example, there are 5 records in a recordset, assuming that you delete the ID 3 data, the remaining record is still 1, 2, 4, 5. This brings a certain amount of trouble. 5, the record set There are two ways to access all the data in the record set, which is available, see: Http://gceclub.sun.com.cn/nasapp/sme/controller/teclist?tid=0103 6, other operations 6.1 Delete Record Set The delete record set is different from the delete record, you need to use the deleteRecordStore method, sample code: ReveteRecordStore ("UserName"); This code deletes a recordset named UserName.