⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writereadexample.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class WriteReadExample extends MIDlet implements CommandListener
{
  private Display display;
  private Alert alert;
  private Form form; 
  private Command exit; 
  private Command start;
  private RecordStore recordstore = null;
  public WriteReadExample()
  {
    display = Display.getDisplay(this);
    exit = new Command("Exit", Command.SCREEN, 1);
    start = new Command("Start", Command.SCREEN, 1);
    form = new Form("Record");
    form.addCommand(exit);
    form.addCommand(start);
    form.setCommandListener(this);
  }
  public void startApp()
  {
    display.setCurrent(form);
  }
  public void pauseApp()
  {
  }
  public void destroyApp( boolean unconditional )
  {
  }
  public void commandAction(Command command, Displayable displayable)
  {
    if (command == exit)
    {
      destroyApp(true);
      notifyDestroyed();
    }
    else if (command == start)
    {
      try
      {
	   //创建或者打开记录存储
       recordstore = RecordStore.openRecordStore(
                                   "myRecordStore", true );
      }
      catch (Exception error)
      {
        alert = new Alert("Error Creating", 
                        error.toString(), null, AlertType.WARNING); 
        alert.setTimeout(Alert.FOREVER); 
        display.setCurrent(alert);
      }
      try
      {
		//声明要存储的数据
        String outputData = "First Record";
        byte[] byteOutputData = outputData.getBytes();
		//添加记录
        recordstore.addRecord(byteOutputData, 0, 
                     byteOutputData.length);
      }
      catch ( Exception error)
      {
        alert = new Alert("Error Writing", 
                     error.toString(), null, AlertType.WARNING); 
        alert.setTimeout(Alert.FOREVER); 
        display.setCurrent(alert);
      }
      try
      { 
		//声明存放记录数据的字节数组
        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, 
                          length), null, AlertType.WARNING); 
          alert.setTimeout(Alert.FOREVER); 
          display.setCurrent(alert); 


      }
      catch (Exception error)
      {
        alert = new Alert("Error Reading", error.toString(), 
                            null, AlertType.WARNING); 
        alert.setTimeout(Alert.FOREVER); 
        display.setCurrent(alert);
      }
      try
      {
		 //关闭记录存储
        recordstore.closeRecordStore();
      }
      catch (Exception error)
      {
        alert = new Alert("Error Closing", error.toString(), 
                         null, AlertType.WARNING); 
        alert.setTimeout(Alert.FOREVER); 
        display.setCurrent(alert);
      }
	  //判断是否还有对记录存储的打开引用
      if (RecordStore.listRecordStores() != null)
      {
        try
        {
		  //删除记录存储
          RecordStore.deleteRecordStore("myRecordStore");
        }
        catch (Exception error)
        {
         alert = new Alert("Error Removing", error.toString(), 
                        null, AlertType.WARNING); 
         alert.setTimeout(Alert.FOREVER); 
         display.setCurrent(alert);
        }
      }      
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -