Home J2me J2ME RMS Read Write
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

J2ME RMS Read Write

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

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

Advertisement

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: December 2, 2008

Ask Questions?    Discuss: J2ME RMS Read Write   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Nsubuga Nicholas
April 17, 2011
quetion

how can i write data to the record store form the j2me form and retrieve it
nufa
January 10, 2012
source code j2me

does the rms create a record (exp: in a .txt file)? If so, where does this record resides?
DMCA.com