RecordStore(rms) write Java Object
A record store is an ordered collection of records. Records are not independent entities: each must belong to a record store, and all record access occurs through the record store. In fact, the record store guarantees that records are read and written atomically, with no possibility of data corruption.
When a record is created, the record store assigns it a unique identifier, an integer called the record ID. The first record added to a record store has a record ID of 1, the second a record ID of 2, and so on. A record ID is notan index: record deletions do not renumber existing records or affect the value of the next record ID.
Names are used to identify record stores within a MIDlet suite. A record store’s name consists of 1 to 32 Unicode characters, and must be unique within the MIDlet suite that created the record store. In MIDP 1.0, record stores cannot be shared by different MIDlet suites. MIDP 2.0 optionally allows a MIDlet suite to share a record store with other suites, in which case the record store is identified by the names of the MIDlet suite and its vendor, along with the record store name itself.
Record stores also maintain time-stamp and version information so applications can discover when a record store was last modified. For close tracking, applications can register a listener to be notified whenever a record store is modified.
At the API level, a record store is represented by an instance of the javax.microedition.rms.RecordStore class. All RMS classes and interfaces are defined in the javax.microedition.rms package.
This Class demonstrates how to write java Object to RecordStore (RMS).
Download NetBeans project files.
package freesrc.com;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
/**
*
* @author freesrc.com
*/
public class RecordStoreWriter {
public static final synchronized Event readEvent(String recordName) {
Event result = null;
RecordStore rec = null;
DataInputStream dis = null;
try {
rec = RecordStore.openRecordStore(recordName, true);
if (rec.getNumRecords() > 0) {
result = new Event();
dis = new DataInputStream(new ByteArrayInputStream(rec.getRecord(1)));
result.setId(dis.readInt());
result.setName(dis.readUTF());
} else {
result = null;
}
} catch (Exception ex) {
ex.printStackTrace();
result = null;
} finally {
if (rec != null) {
try {
rec.closeRecordStore();
} catch (Exception e) {
result = null;
}
rec = null;
}
if (dis != null) {
try {
dis.close();
} catch (Exception e) {
}
rec = null;
}
}
return result;
}
public static synchronized void writeEvent(Event event, String recordName) {
RecordStore rec = null;
ByteArrayOutputStream bos = null;
DataOutputStream dos = null;
try {
deleteRecord(recordName);
bos = new ByteArrayOutputStream();
dos = new DataOutputStream(bos);
dos.writeInt(event.getId());
dos.writeUTF(event.getName());
dos.flush();
rec = RecordStore.openRecordStore(recordName, true);
byte[] b = bos.toByteArray();
rec.addRecord(b, 0, b.length);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (rec != null) {
try {
rec.closeRecordStore();
} catch (Exception e) {
}
rec = null;
}
if (dos != null) {
try {
dos.close();
} catch (IOException ex) {
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException ex) {
}
}
}
}
//===delete record if exits
public static synchronized void deleteRecord(String name) {
String[] array = RecordStore.listRecordStores();
if (array != null) {
for (int i = 0; i < array.length; i++) {
if (name.equals(array[i])) {
try {
RecordStore.deleteRecordStore(array[i]);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
}
}
Leave a Reply
You must be logged in to post a comment.
RSS