J2ME road——J2ME实现RMS手机的存储

来源:互联网 发布:淘宝网首页茵曼 编辑:程序博客网 时间:2024/05/30 04:39

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

public class SimpleRMS extends MIDlet{
 
 private RecordStore rs;
 private static final String STORE_NAME = "My Record Store";
 
 public SimpleRMS() throws Exception
 {
  rs = RecordStore.openRecordStore(STORE_NAME, true);
  //Create some records in the store
  String[] words = {"they", "mostly", "come", "at", "night" };
  for(int i=0; i<words.length; i++)
  {
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   DataOutputStream dataOutputStream= new DataOutputStream(byteArrayOutputStream);
   dataOutputStream.writeUTF(words[i]);
   
      //add another dataOutputStream.writeXXX statements if you like
   dataOutputStream.flush();
   
   
   //add the record
   byte[] recordOut =byteArrayOutputStream.toByteArray();
   int newRecordId = rs.addRecord(recordOut, 0, recordOut.length);
   System.out.println("Adding new record;"+ newRecordId + "value:" +recordOut);
   
   dataOutputStream.close();
   byteArrayOutputStream.close();
  }
   
   System.out.println("Record store now has" +rs.getNumRecords() +
        "record(s) using" +rs.getSize() +"byte(s)"+
        "[" +rs.getSizeAvailable() +"bytes free]");
   //retrieve the records
   for(int i =1;i<= rs.getNumRecords(); i++)
   {
    int recordSize = rs.getRecordSize(i);
    if(recordSize >0)
    {
     ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(rs.getRecord(i));
     DataInputStream dataInputStream = new DataInputStream(byteArrayInputStream);
     
     String value = dataInputStream.readUTF();
     
     System.out.println("Retrieved record;" + i+ "value:" +value);
     dataInputStream.close();
     byteArrayInputStream.close();
    }
   
   }
  
 }

 protected void destroyApp(boolean b) throws MIDletStateChangeException {
  // TODO 自动生成方法存根
  
 }

 protected void pauseApp() {
  // TODO 自动生成方法存根
  
 }

 protected void startApp() throws MIDletStateChangeException {
  // TODO 自动生成方法存根
  destroyApp(false);
  notifyDestroyed();
 }
 

}