Home J2me J2ME Record Store Example


Share on Google+Share on Google+

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


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");

  public void startApp(){

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){

  public void commandAction(Command command, 
  Displayable displayable

  if(command == exit){
  }else if(command == start){
  recordStore = 
("Sandeep Kumar Suman"true );
  String outputData = "First Record Completed...";
  byte[] byteOutputData = outputData.getBytes();
  recordStore.addRecord(byteOutputData, 0
  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
), null, AlertType.WARNING)
  }catch (Exception e){}

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

Download Source Code


If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 20, 2008

Related Tutorials

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

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
August 18, 2013

/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.util.Vector; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; import javax.microedition.lcdui.ChoiceGroup; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.*; import javax.microedition.rms.InvalidRecordIDException; import javax.microedition.rms.RecordEnumeration; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreNotOpenException; /** * @author Lukewarm */ public class RecStr extends MIDlet implements CommandListener { Form f, f1; Display d; TextField t1, t2, t3; Command c1, c2, c3, c4; ChoiceGroup v1; RecordStore rc; Vector vect; public RecStr() throws RecordStoreException { d = Display.getDisplay(this); f = new Form("Add Contact"); f1 = new Form("Add Form"); t1 = new TextField("First Name", null, 20, TextField.ANY); t2 = new TextField("Last Name", null, 20, TextField.ANY); f.append(t1); f.append(t2); c1 = new Command("Add", Command.BACK, 0); c2 = new Command("Back", Command.BACK, 0); c3 = new Command("Delete", Command.BACK, 0); c4 = new Command("ADD", Command.BACK, 0); f1.addCommand(c1); f1.addCommand(c3); f.addCommand(c2); f.addCommand(c4); f.setCommandListener(this); f1.setCommandListener(this); v1 = new ChoiceGroup("Contact", ChoiceGroup.EXCLUSIVE); f1.append(v1); rc = RecordStore.openRecordStore("contac", true); vect = new Vector(); // loadata(); } public void startApp() { d.setCurrent(f1); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c == c1) { Display.getDisplay(this).setCurrent(f); } if (c == c2) { Display.getDisplay(this).setCurrent(f1); } if (c == c4) { String tffn = t1.getString().trim(); String tfln = t2.getString().trim(); try { String rcst = tffn + " " + tfln; byte[] rec = rcst.getBytes(); rc.addRecord(rec, 0, rec.length); t1.setString(""); t2.setString(""); Alert al = new Alert("Success", "Successfully Saved", null, AlertType.ALARM); Display.getDisplay(this).setCurrent(al); // loadata(); } catch (Exception e) { } } if (c == c3) { try { int ind = v1.getSelectedIndex(); String rerds = (String) vect.elementAt(ind); int i = Integer.parseInt(rerds); rc.deleteRecord(i); Alert al = new Alert("Success", "Successfully Deleted", null, AlertType.ALARM); Display.getDisplay(this).setCurrent(al); loadata(); } catch (RecordStoreException ex) { ex.printStackTrace(); } } } public void loadata() throws RecordStoreException { try { v1.deleteAll(); RecordEnumeration rcv = rc.enumerateRecords(null, null, false); while (rcv.hasNextElement()) { int cn = rcv.nextRecordId(); vect.addElement("" + cn); byte[] tr = rc.getRecord(cn); String rty = new String(tr); // rty = rty.substring(0,rty.indexOf(" ")); v1.append(rty, null); } } catch (InvalidRecordIDException ex) { ex.printStackTrace(); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); } } }