▼MIDP2.0メモ▼
共有レコードストアの読み書き


レコードストアの読み書きを行うプログラム。エミュレータだとうまく動かず。702NKで動作確認。


  


プログラム
SheardWriteEx.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

//共有レコードストアの書き込み
public class SharedWriteEx extends MIDlet
    implements CommandListener {
    TextBox textBox;//テキストボックス

    //コンストラクタ
    public SharedWriteEx(){
        //テキストボックス
        textBox=new TextBox("SharedWriteEx","",255,0);
        textBox.addCommand(new Command("書込",Command.SCREEN,1));
        textBox.setCommandListener(this);
        (Display.getDisplay(this)).setCurrent(textBox);
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean unconditional) {
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        //共有レコードストアの書き込み
        if (c.getPriority()==1){
            writeSharedRecordStore("text",textBox.getString());
        }
    }

    //共有レコードストアの書き込み
    void writeSharedRecordStore(String name,String str) {
        byte[] w;
        RecordStore rs=null;
        try {
            //文字列→バイトデータ変換
            w=str.getBytes("EUC-JP");
            //レコードストアと接続
            rs=RecordStore.openRecordStore(name,true,
                RecordStore.AUTHMODE_ANY,false);
            //レコードの新規作成
            if (rs.getNumRecords()==0) {
                rs.addRecord(w,0,w.length);
            }
            //レコードの上書き
            else {
                rs.setRecord(1,w,0,w.length);
            }
            //レコードストアと切断
            rs.closeRecordStore();
        } catch (Exception e) {
            try {
                if (rs!=null) rs.closeRecordStore();
            } catch (Exception e2) {
            }
            showErrorDialog("共有レコードストアの書き込みに失敗しました。");
        }
    }

    //エラーダイアログの表示
    void showErrorDialog(String text) {
        Alert alert=new Alert("エラー",text,null,AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        (Display.getDisplay(this)).setCurrent(alert);
    }
}


SheardReadEx.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

//共有レコードストアの読み込み
public class SharedReadEx extends MIDlet
    implements CommandListener {
    TextBox textBox;//テキストボックス

    //コンストラクタ
    public SharedReadEx(){
        //テキストボックス
        textBox=new TextBox("SharedReadEx","",255,0);
        textBox.addCommand(new Command("読込",Command.SCREEN,1));
        textBox.setCommandListener(this);
        (Display.getDisplay(this)).setCurrent(textBox);
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean flag) {
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        //共有レコードストアの読み込み
        if (c.getPriority()==1) {
            String str=readSharedRecordStore("text");
            if (str!=null) textBox.setString(str);
        }
    }

    //共有レコードストアの読み込み
    String readSharedRecordStore(String name) {
        byte[] w=null;
        RecordStore rs=null;
        try {
            //レコードストアと接続
            rs=RecordStore.openRecordStore(name,"Unknown","SharedWriteEx");
            //レコードの読み込み
            w=rs.getRecord(1);
            //レコードストアと切断
            rs.closeRecordStore();
            //バイトデータ→文字列変換
            return new String(w,"EUC-JP");
        } catch (Exception e) {
            try {
                if (rs!=null) rs.closeRecordStore();
            } catch (Exception e2) {
            }
            showErrorDialog("共有レコードストアの読み込みに失敗しました。");
        }
        return null;
    }

    //エラーダイアログの表示
    void showErrorDialog(String text) {
        Alert alert=new Alert("エラー",text,null,AlertType.ERROR);
        alert.setTimeout(Alert.FOREVER);
        (Display.getDisplay(this)).setCurrent(alert);
    }
}



−戻る−