|
Record Management System for storing/retrieving data on mobile |
|
|
Midlets can be programmed to store data at runtime with RMS mechanism. The package
javax.microedition.rms is used to implement the functionality of RMS APIs.
This package provides facilities for storing, getting, deleting and comparing data.
Some methods are written below to demonstrates some functionalities of RMS:
/* Open database */
public void startApp()
{
try
{
RecordStore recordStore = RecordStore.openRecordStore("file", true,
RecordStore.AUTHMODE_ANY,true);
}
catch(RecordStoreException rse)
{
rse.printStackTrace();
}
}
/* Close database */
public void close() throws RecordStoreNotOpenException,RecordStoreException
{
String fileName = recordStore.getName();
recordStore.closeRecordStore();
recordStore.deleteRecordStore(fileName);
}
/* Enter data */
public void enterData()
{
byte[] rec;
String str = "hello";
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baos);
try
{
outputStream.writeUTF(str);
outputStream.flush();
rec = baos.toByteArray();
recordStore.addRecord(rec,0,rec.length);
baos.reset();
outputStream.close();
outputStream = null;
}
catch (IOException e) {
e.printStackTrace();
outputStream =null;
}
catch(RecordStoreException e) {
e.printStackTrace();
outputStream =null;
}
}
/* Enumerate data */
public synchronized RecordEnumeration enumerate()
throws RecordStoreNotOpenException
{
return recordStore.enumerateRecords(null, null, false);
}
/* Get data */
public void getdata()
{
RecordEnumeration recEnum;
byte[] rec = null;
String str = null;
try {
recEnum = enumerate();
while (recEnum.hasNextElement())
{
rec = recEnum.nextRecord();
ByteArrayInputStream bin = new ByteArrayInputStream( rec);
DataInputStream din = new DataInputStream( bin );
str = din.readUTF();
}
}
catch (Exception e) {}
}
/* Compare data */
public class dataComaparator implements RecordComaparator
{
public int compare(byte[] rec1,byte[] rec2)
{
int compVal;
String rec1,rec2;
String value;
try {
ByteArrayInputStream bin = new ByteArrayInputStream( rec1 );
DataInputStream din = new DataInputStream( bin );
rec1 = din.readUTF();
bin = new ByteArrayInputStream( rec2 );
din = new DataInputStream( bin );
rec2 = din.readUTF();
compVal= rec1.compareTo(rec2);
bin.reset();
din.close();
din =null;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
|
Related Tips
|
Page 1 of 0 ( 0 comments )
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.