writereadexample.java

来自「利用J2ME编制的记录存储体的读写过程实例」· Java 代码 · 共 79 行

JAVA
79
字号
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("myRecordStroe",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 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("Erroe Removing",error.toString(),null,AlertType.WARNING);
          alert.setTimeout(Alert.FOREVER);
          display.setCurrent(alert);
        }
      }
    }
   }
}

⌨️ 快捷键说明

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