Java by API/javax.microedition.rms/RecordStore — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:22, 31 мая 2010

RecordStore: deleteRecordStore(String value) throws RecordStoreException, RecordStoreNotFoundException

 
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
public class deleteRecordStoreMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand;
  private Command confirmCommand;
  private Display display;
  public deleteRecordStoreMIDlet() {
    display = Display.getDisplay(this);
    exitCommand = new Command("exit", Command.EXIT, 1);
    confirmCommand = new Command("confirm", Command.OK, 1);
  }
  public void startApp() {
    TextBox aTextBox = new TextBox("RecordStore", null, 256, TextField.ANY);
    aTextBox.addCommand(exitCommand);
    aTextBox.addCommand(confirmCommand);
    aTextBox.setCommandListener(this);
    display.setCurrent(aTextBox);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
      destroyApp(false);
      notifyDestroyed();
    } else {
      try {
        RecordStore.deleteRecordStore("aRS");
      } catch (Exception e) {
      }
    }
  }
}





RecordStore: enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated) throws RecordStoreNotOpenException

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class J2MESortExample extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form = new Form("Mixed RecordEnumeration", null);
  private Command exit = new Command("Exit", Command.SCREEN, 1);
  private Command start = new Command("Start", Command.SCREEN, 1);
  private RecordStore recordstore = null;
  private RecordEnumeration recordEnumeration = null;
  private Comparator comparator = null;
  public J2MESortExample() {
    display = Display.getDisplay(this);
    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);
        String outputData[] = { "M", "B", "A" };
        for (int x = 0; x < 3; x++) {
          byte[] byteOutputData = outputData[x].getBytes();
          recordstore.addRecord(byteOutputData, 0, byteOutputData.length);
        }
        StringBuffer buffer = new StringBuffer();
        Comparator comparator = new Comparator();
        recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
        while (recordEnumeration.hasNextElement()) {
          buffer.append(new String(recordEnumeration.nextRecord()));
          buffer.append("\n");
        }
        alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
        recordstore.closeRecordStore();
        if (RecordStore.listRecordStores() != null) {
          RecordStore.deleteRecordStore("myRecordStore");
          recordEnumeration.destroy();
        }
      } catch (Exception error) {
        alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
  }
}
class Comparator implements RecordComparator {
  public int compare(byte[] record1, byte[] record2) {
    String string1 = new String(record1), string2 = new String(record2);
    int comparison = string1.rupareTo(string2);
    if (comparison == 0)
      return RecordComparator.EQUIVALENT;
    else if (comparison < 0)
      return RecordComparator.PRECEDES;
    else
      return RecordComparator.FOLLOWS;
  }
}





RecordStore: getNumRecords() throws RecordStoreNotOpenException

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
public class J2MEWriteReadExample extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form = new Form("Record");
  private Command exit = new Command("Exit", Command.SCREEN, 1);
  private Command start = new Command("Start", Command.SCREEN, 1);
  private RecordStore recordstore = null;
  public J2MEWriteReadExample() {
    display = Display.getDisplay(this);
    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);
        String outputData = "First Record";
        byte[] byteOutputData = outputData.getBytes();
        recordstore.addRecord(byteOutputData, 0, byteOutputData.length);
        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);
        recordstore.closeRecordStore();
        if (RecordStore.listRecordStores() != null) {
          RecordStore.deleteRecordStore("myRecordStore");
        }
      } catch (Exception error) {
        alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
  }
}





RecordStore: getRecord(int arg0, byte[] arg1, int arg2) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
public class J2MEWriteReadExample extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form = new Form("Record");
  private Command exit = new Command("Exit", Command.SCREEN, 1);
  private Command start = new Command("Start", Command.SCREEN, 1);
  private RecordStore recordstore = null;
  public J2MEWriteReadExample() {
    display = Display.getDisplay(this);
    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);
        String outputData = "First Record";
        byte[] byteOutputData = outputData.getBytes();
        recordstore.addRecord(byteOutputData, 0, byteOutputData.length);
        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);
        recordstore.closeRecordStore();
        if (RecordStore.listRecordStores() != null) {
          RecordStore.deleteRecordStore("myRecordStore");
        }
      } catch (Exception error) {
        alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
  }
}





RecordStore: getRecordSize(int value) throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
public class J2MEWriteReadExample extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form = new Form("Record");
  private Command exit = new Command("Exit", Command.SCREEN, 1);
  private Command start = new Command("Start", Command.SCREEN, 1);
  private RecordStore recordstore = null;
  public J2MEWriteReadExample() {
    display = Display.getDisplay(this);
    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);
        String outputData = "First Record";
        byte[] byteOutputData = outputData.getBytes();
        recordstore.addRecord(byteOutputData, 0, byteOutputData.length);
        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);
        recordstore.closeRecordStore();
        if (RecordStore.listRecordStores() != null) {
          RecordStore.deleteRecordStore("myRecordStore");
        }
      } catch (Exception error) {
        alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
        alert.setTimeout(Alert.FOREVER);
        display.setCurrent(alert);
      }
    }
  }
}





RecordStore: listRecordStores()

 
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
public class J2MERecordStoreExample extends MIDlet implements CommandListener {
  private Display display;
  private Alert alert;
  private Form form = new Form("Record Store");
  private Command exit = new Command("Exit", Command.SCREEN, 1);
  private Command start = new Command("Start", Command.SCREEN, 1);
  private RecordStore recordstore = null;
  private RecordEnumeration recordenumeration = null;
  public J2MERecordStoreExample() {
    display = Display.getDisplay(this);
    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 {
        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);
        }
      }
    }
  }
}





RecordStore: openRecordStore(String value, boolean arg1) throws RecordStoreException, RecordStoreFullException, RecordStoreNotFoundException

 

import javax.microedition.lcdui.rumand;
import javax.microedition.lcdui.rumandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;
public class getRecordMIDlet extends MIDlet implements CommandListener {
  private Command exitCommand = new Command("exit", Command.STOP, 1);
  private Display display;
  public getRecordMIDlet() {
    display = Display.getDisplay(this);
  }
  public void startApp() {
    TextBox aTextBox = new TextBox("Main", null, 256, TextField.ANY);
    RecordStore rs = null;
    boolean existingOrNot = false;
    existingOrNot = existing("aRS");
    if (existingOrNot) {
      try {
        rs = RecordStore.openRecordStore("aRS", false);
      } catch (Exception e) {
      }
    } else {
      try {
        rs = RecordStore.openRecordStore("aRS", true);
      } catch (Exception e) {
      } finally {
      }
    }
    try {
      String record = "";
      for (int i = 1; i < rs.getNextRecordID(); i++) {
        record = new String(rs.getRecord(i));
      }
      aTextBox.setString(record);
    } catch (Exception e) {
      aTextBox.setString("Failed");
      try {
        rs.closeRecordStore();
        RecordStore.deleteRecordStore("aRS");
      } catch (Exception x) {
      }
    }
    try {
      rs.closeRecordStore();
    } catch (Exception e) {
    }
    aTextBox.addCommand(exitCommand);
    aTextBox.setCommandListener(this);
    display.setCurrent(aTextBox);
  }
  public void pauseApp() {
  }
  public void destroyApp(boolean unconditional) {
  }
  public boolean existing(String recordStoreName) {
    boolean existingOrNot = false;
    RecordStore rs = null;
    if (recordStoreName.length() > 32)
      return false;
    try {
      rs = RecordStore.openRecordStore(recordStoreName, false);
    } catch (RecordStoreNotFoundException e) {
      existingOrNot = false;
    } catch (Exception e) {
    } finally {
      try {
        rs.closeRecordStore();
      } catch (Exception e) {
      }
    }
    return existingOrNot;
  }
  public void commandAction(Command c, Displayable s) {
    destroyApp(false);
    notifyDestroyed();
  }
}