J2ME RMS Read Write

This Application specially tries to explain how to read and write the data using RecordStore class.

J2ME RMS Read Write

J2ME RMS Read Write

     

This Application specially tries to explain how to read and write the data using RecordStore class. In the RecordStore class the following methods are available:

  • addRecord(byte[] data, int offset, int numBytes) 
  • addRecordListener(RecordListener listener)
  • closeRecordStore()
  • deleteRecord(int recordId)
  • deleteRecordStore(String recordStoreName)
  • enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) 
  • getLastModified()
  • getName()
  • getNextRecordID()
  • getNumRecords()
  • getRecord(int recordId)
  • getRecord(int recordId, byte[] buffer, int offset)
  • getRecordSize(int recordId)
  • getSize()
  • getSizeAvailable()
  • getVersion()
  • listRecordStores()
  • openRecordStore(String recordStoreName, boolean createIfNecessary)
  • removeRecordListener(RecordListener listener)
  • setRecord(int recordId, byte[] newData, int offset, int numBytes)

The Application is as follows:

 

ReadWriteRMS.java

 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.rms.*;

public class ReadWriteRMS extends MIDlet{
  private RecordStore rs = null;
  static final String REC_STORE = "ReadWriteRMS";

  public void startApp(){
  openRecStore();
  writeRecord("Core J2ME Technology");
  writeRecord("J2ME Wireless Toolkit");  
  readRecords();
  closeRecStore();
  deleteRecStore()
  }

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){
  notifyDestroyed();
  }

  public void openRecStore(){
  try{
  rs = RecordStore.openRecordStore(REC_STORE, true );
  }catch (Exception e){}
  }  

  public void closeRecStore(){
  try{
  rs.closeRecordStore();
  }catch (Exception e){}
  }

  public void deleteRecStore(){
  if (RecordStore.listRecordStores() != null){
  try{
  RecordStore.deleteRecordStore(REC_STORE);
  }catch (Exception e){}
  }  
  }

  public void writeRecord(String str){
  byte[] rec = str.getBytes();
  try{
  rs.addRecord(rec, 0, rec.length);
  }catch (Exception e){}
  }

  public void readRecords(){
  try{
  byte[] recData = new byte[5]
  int len;
  
  for(int i = 1; i <= rs.getNumRecords(); i++){
  if(rs.getRecordSize(i> recData.length){
  recData = new byte[rs.getRecordSize(i)];
  }
  len = rs.getRecord(i, recData, 0);
  System.out.println("------------------------------");
  System.out.println("Record " + i + " : " +
    
new String(recData, 0, len));
  System.out.println("------------------------------");  
  }
  }catch (Exception e){}
  }
}

 

Download Source Code