J2ME Record Store Example

In this Midlet, we are going to read string data and write by using the RecordStore class.

J2ME Record Store Example

J2ME Record Store Example

     

In this Midlet, we are going to read string data and write by using the RecordStore class. The package of the RecordStore class is the javax.microedition.rms. In our program we have used a byte[] to read the data. In J2ME a record store consists of a collection of records and that records remain persistent across multiple invocations of the MIDlet.

 

 

 

 

 

Output of the Record Store Example..

Source Code of ReadWriteMIDlet.java

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

public class ReadWriteMIDlet extends MIDlet implements CommandListener{
  private Display display;
  private Alert alert;
  private Form form; 
  private Command exit, start; 
  private RecordStore recordStore;

  public ReadWriteMIDlet(){
  display = Display.getDisplay(this);
  start = new Command("Start", Command.SCREEN, 1);
  exit = new Command("Exit", Command.SCREEN, 1);
  form = new Form("Record");
  form.addCommand(start);
  form.addCommand(exit);  
  form.setCommandListener(this);
  }

  public void startApp(){
  display.setCurrent(form);
  }

  public void pauseApp(){}

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

  public void commandAction(Command command, 
  Displayable displayable
){

  if(command == exit){
  destroyApp(true);
  }else if(command == start){
  try{
  recordStore = 
  RecordStore.openRecordStore
("Sandeep Kumar Suman"true );
  
  String outputData = "First Record Completed...";
  byte[] byteOutputData = outputData.getBytes();
  recordStore.addRecord(byteOutputData, 0
  byteOutputData.length
);
  
  byte[] byteInputData = new byte[1]
  int length = 0;
  for (int x = 1; x <= recordStore.getNumRecords(); x++){
  if (recordStore.getRecordSize(x> byteInputData.length){
  byteInputData = new byte[recordStore.getRecordSize(x)];
  }
  length = recordStore.getRecord(x, byteInputData, 0);
  }
  alert = new Alert("Reading"new String(byteInputData, 0
  length
), null, AlertType.WARNING)
  alert.setTimeout(Alert.FOREVER)
  display.setCurrent(alert)
  
  recordStore.closeRecordStore();
  }catch (Exception e){}

  if(RecordStore.listRecordStores() != null){
  try{
  RecordStore.deleteRecordStore("Sandeep Kumar Suman");
  }catch (Exception e){}
  }  
  }
  }
}

Download Source Code