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


レコードストアの読み書きを行うプログラム。





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

//レコードストアの読み書き
public class RecordStoreEx extends MIDlet {
    static RecordStoreEx  a;//本体
    static RecordStoreBox b;//テキストボックス

    //コンストラクタ
    public RecordStoreEx(){
        a=this;
        b=new RecordStoreBox();
        (Display.getDisplay(this)).setCurrent(b);
    }

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

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

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

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

//レコードストアの読み書き(テキストボックス)
public class RecordStoreBox extends TextBox
    implements CommandListener {

    //コンストラクタ
    public RecordStoreBox(){
        super("RecordStoreEx","",255,0);
        addCommand(new Command("書込",Command.SCREEN,1));
        addCommand(new Command("読込",Command.SCREEN,2));
        setCommandListener(this);
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        //レコードストアの書き込み
        if (c.getPriority()==1){
            try {
                writeRS("test",getString().getBytes("UTF-8"));
            } catch (Exception e) {
                Dialog.showDialog("エラー",
                    "レコードストアの書き込みに失敗しました。",
                    RecordStoreEx.a,this);
            }
        }
        //レコードストアの読み込み
        if (c.getPriority()==2) {
            try {
                setString(new String(readRS("test"),"UTF-8"));
            } catch (Exception e) {
                Dialog.showDialog("エラー",
                    "レコードストアの読み込みに失敗しました。",
                    RecordStoreEx.a,this);
            }
        }
    }

    //レコードストアの書き込み
    private void writeRS(String name,byte[] data) throws Exception {
        RecordStore rs=null;
        try {
            //レコードストアとの接続
            rs=RecordStore.openRecordStore(name,true);

            //レコードの書き込み
            if (rs.getNumRecords()==0) {
                rs.addRecord(data,0,data.length);
            } else {
                rs.setRecord(1,data,0,data.length);
            }

            //レコードストアとの切断
            rs.closeRecordStore();
        } catch (Exception e) {
            try {
                if (rs!=null) rs.closeRecordStore();
            } catch (Exception e2) {
            }
            throw e;
        }
    }

    //レコードストアの読み込み
    private byte[] readRS(String name) throws Exception {
        byte[] data=null;
        RecordStore rs=null;
        try {
            //レコードストアとの接続
            rs=RecordStore.openRecordStore(name,false);

            //レコードの読み込み
            data=rs.getRecord(1);

            //レコードストアとの切断
            rs.closeRecordStore();
            return data;
        } catch (Exception e) {
            try {
                if (rs!=null) rs.closeRecordStore();
            } catch (Exception e2) {
            }
            throw e;
        }
    }
}


Dialog.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

//ダイアログ
public class Dialog extends Form 
    implements CommandListener {
    public static int BUTTON_CANCEL     =0x0002;
    public static int BUTTON_NO         =0x0008;
    public static int BUTTON_OK         =0x0001;
    public static int BUTTON_YES        =0x0004;
    public static int DIALOG_ERROR      =2;
    public static int DIALOG_INFO       =0;
    public static int DIALOG_WARNING    =1;
    public static int DIALOG_YESNO      =3;
    public static int DIALOG_YESNOCANCEL=4;
    
    //GUI
    private int         type;   //種別
    private int         result; //結果
    private MIDlet      midlet; //本体
    private Displayable disp;   //戻り先
    private StringItem  label;  //ラベル
    private Command[]   softKey;//ソフトキー

    //コンストラクタ
    public Dialog(int type,String title,MIDlet midlet,Displayable disp){
        super(title);
        
        //GUI
        this.type  =type;
        this.result=-999;
        this.midlet=midlet;
        this.disp  =disp;
        label=new StringItem("","");
        append(label);

        //ソフトキーの追加
        setCommandListener(this);
        softKey=new Command[2];
        if (type==DIALOG_INFO || type==DIALOG_WARNING) {
            softKey[0]=new Command("OK",Command.SCREEN,0);
            addCommand(softKey[0]);
        } else if (type==DIALOG_YESNO || type==DIALOG_YESNOCANCEL) {
            softKey[0]=new Command("はい",  Command.SCREEN,0);
            softKey[1]=new Command("いいえ",Command.SCREEN,1);
            addCommand(softKey[0]);
            addCommand(softKey[1]);
        }
    }

    //テキストの指定
    public void setText(String text) {
        label.setText(text);
    }
    
    //表示
    public int show() {
        Display.getDisplay(midlet).setCurrent(this);
        while (result>=0) {
            try {
                Thread.sleep(200);
            } catch (Exception e) {
            }
        }
        return result;
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable disp) {
        if (type==DIALOG_INFO || type==DIALOG_WARNING) {
            if (c==softKey[0]) result=BUTTON_OK;
            Display.getDisplay(midlet).setCurrent(this.disp);
        } else if (type==DIALOG_YESNO || type==DIALOG_YESNOCANCEL) {
            if (c==softKey[0]) result=BUTTON_YES;
            if (c==softKey[1]) result=BUTTON_NO;
            Display.getDisplay(midlet).setCurrent(this.disp);
        }
    }
    
    //メッセージダイアログの表示
    public static void showDialog(String title,String msg,
        MIDlet midlet,Displayable disp){
        Dialog dlg=new Dialog(Dialog.DIALOG_INFO,title,midlet,disp);
        dlg.setText(msg);
        dlg.show();
    }

    //Yes/Noダイアログの表示
    public static int showYNDialog(String title,String msg,
        MIDlet midlet,Displayable disp){
        Dialog dlg=new Dialog(Dialog.DIALOG_YESNO,title,midlet,disp);
        dlg.setText(msg);
        return dlg.show();
    }
}


JADファイルとMANIFESTファイル
RecordStoreEx.jad
MIDlet-1: RecordStoreEx, , RecordStoreEx
MIDlet-Jar-Size: 4385
MIDlet-Jar-URL: RecordStoreEx.jar
MIDlet-Name: RecordStoreEx
MIDlet-Vendor: My Vendor
MIDlet-Version: 1.0
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-Application-Range: 0,0
MIDlet-Data-Size: 1024



−戻る−