如何在MIDlet中将字符数组数据读取和写入到RMS

下边的代码说明了如何在MIDlet中将字符数组数据读取和写入到RMS的RecortStore中,每个RecordStore包括id,长度和数组数据

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 destroyApp( boolean unconditional )
  {}

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

  public void pauseApp(){}
 
  /* 打开RecordStore */
  public void open()
  {
    try
    {
      rs = RecordStore.openRecordStore(RECORD_STORE, true );
    }
    catch (Exception e)
    {}
  }   

  /* 关闭RecordStore*/
  public void close()
  {
    try
    {
      rs.closeRecordStore();
    }
    catch (Exception e)
    {}
  }
 
  /* 删除RecordStore*/
  public void delete()
  {
    if (RecordStore.listRecordStores() != null)
    {
      try
      {
        RecordStore.deleteRecordStore(RECORD_STORE);
      }
      catch (Exception e)
      {}
    }     
  }

  /* 写入数据*/
  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);
  }

  /* 写入数据到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]); // 写入id
        String str[] = (String[])data[i];
        ds.writeInt(str.length); // 写入数据长度
       
        for (int j=0;j<str.length;j++)
        {
          ds.writeUTF(str[j]); // 写入数组数据
        }
       
        ds.flush();
        record = baos.toByteArray();
       
        // 添加数据到RecordStore
        rs.addRecord(record, 0, record.length);     
        baos.reset();
      }
     
      baos.close();
      ds.close();
    }
    catch (Exception e)
    {}
  }

  /* 从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 =0 ;j<length;j++)
        {
          System.out.println("Data: " +ds.readUTF());
        }
        System.out.println("--------------------");       
        bais.reset();
      }
      bais.close();
      ds.close();
    }     
    catch (Exception e)
    {}
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值