Home J2me J2ME Record Store Example



J2ME Record Store Example
Posted on: November 20, 2008 at 12:00 AM
In this Midlet, we are going to read string data and write by using the RecordStore class.

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

Related Tags for J2ME Record Store Example:
javacstringclassdataj2meiomultiplesedipcollectionreadusingbytethisidnsisaipackagewriteeditrecordprogramrecordstoidldstedimidletramstoreidleemainitcrossaxmultirsimidluseulpejavaxinpersistentrmasmnttroscaosletjpackadclrmsstsemagepersistinvocationmeicrproackcattorsvaxtipeditioncolatpacosskishallmicrcollectgoeaandarcollectconsstrrdsvassriringrdrecordstorethavstatihatiplpleplprmindonogrolo


More Tutorials from this section

Ask Questions?    Discuss: J2ME Record Store Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.