java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Reading and writing array data to persistent storage E-mail
User Rating: / 20
PoorBest 

Package javax.microedition.rms facilitates the persistent storage of data. This feature is available in MIDP profile. So you can use it safely in your midlets.

The example below demonstrates how to read and write String array data. Each record store has id, length and array data.

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

public class arrayStore extends MIDlet
{
  private RecordStore rs = null;
  static final String RECORD_STORE = "rmstest";

  public arrayStore()
  {
    open();
    writeData();
    readData()
    close();   
    delete();  
  }

  public void destroyAppboolean unconditional )
  {}

  public void startApp()
  {
    destroyApp(false);
    notifyDestroyed();
  }

  public void pauseApp(){}
  
  /* Open RMS with filename */
  public void open()
  {
    try
    {
      rs = RecordStore.openRecordStore(RECORD_STORE, true );
    }
    catch (Exception e)
    {}
  }    

  /* Close RMS*/
  public void close()
  {
    try
    {
      rs.closeRecordStore();
    }
    catch (Exception e)
    {}
  }
  
  /* Delete RMS with filename*/
  public void delete()
  {
    if (RecordStore.listRecordStores() != null)
    {
      try
      {
        RecordStore.deleteRecordStore(RECORD_STORE);
      }
      catch (Exception e)
      {}
    }      
  }

  /* Write String data with id assigned to each data*/
  public void writeData()
  {
    int ids[] {1,2,3};
    String str1[] {"A""B""C"};
    String str2[] {"D""E""F"};
    String str3[] {"G""H""I"};
    Object data[] new Object[3];
    data[0=str1;
    data[1]= str2;
    data[2]= str3; 
    writeStream(ids,data);
  }

  /* Write data to RMS */
  public void writeStream(int[] ids,Object[] data)
  {
    try
    {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      DataOutputStream ds = new DataOutputStream(baos);
      byte[] record;

      for (int i = 0; i < ids.length; i++)
      {
        ds.writeInt(ids[i])// write id
        String str[] (String[])data[i];
        ds.writeInt(str.length)// write length of data
        
        for (int j=0;j<str.length;j++)
        {
          ds.writeUTF(str[j])// write array data
        }
        
        ds.flush();
        record = baos.toByteArray();
        
        // Add record data to recordstore
        rs.addRecord(record, 0, record.length);      
        baos.reset();
      }
      
      baos.close();
      ds.close();
    }
    catch (Exception e)
    {}
  }

  /* Read data from recordstore*/
  public void readData()
  {
    try
    {
      byte[] recData = new byte[50];
      ByteArrayInputStream bais = new ByteArrayInputStream(recData);
      DataInputStream ds = new DataInputStream(bais);

      for (int i = 1; i <= rs.getNumRecords(); i++)
      {
        // Get data into the byte array
        rs.getRecord(i, recData, 0);

        System.out.println("Record #" + i);        
        System.out.println("ID: " + ds.readInt());
        int length = ds.readInt();
        System.out.println("Length: " + length);
        for(int j =;j<length;j++)
        {
          System.out.println("Data: " +ds.readUTF());
        }
        System.out.println("--------------------");        
        bais.reset();
      }
      bais.close();
      ds.close();
    }      
    catch (Exception e)
    {}
  }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.