▼MIDP2.0メモ▼
端末内やSDカード内のファイルにアクセスする


端末内部のデータフォルダ(/ms)やメモリーカードのデータフォルダ(/mc)に保存するプログラムを作る。

ルート直下にはファイルは生成できない。フォルダを生成後、その下にファイルを生成する。フォルダは2段までOK。
端末内部 file:///ms/フォルダ/ファイル名
file:///ms/フォルダ/フォルダ/ファイル名
メモリカード file:///mc/フォルダ/ファイル名
file:///mc/フォルダ/フォルダ/ファイル名




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

//端末内やSDカード内のファイルにアクセスする
public class StorageConnectionEx extends MIDlet {
    static StorageConnectionEx  a;//本体
    static StorageConnectionBox b;//テキストボックス

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

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

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

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

StorageConnectionBox.java
import com.j_phone.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;
import java.io.*;

//ファイルの読み書き(テキストボックス)
public class StorageConnectionBox extends TextBox
    implements CommandListener {

    //コンストラクタ
    public StorageConnectionBox(){
        super("StorageConnectionEx","",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 {
                writeFile("file:///ms/test/test.txt",
                    getString().getBytes());
            } catch (Exception e) {
                Dialog.showDialog("エラー",
                    "ファイルの書き込みに失敗しました。",
                    StorageConnectionEx.a,this);
            }

            
        }
        //ファイルの読み込み
        if (c.getPriority()==2) {
            try {
                setString(new String(readFile(
                    "file:///ms/test/test.txt")));
            } catch (Exception e) {
                Dialog.showDialog("エラー",
                    "ファイルの読み込みに失敗しました。",
                    StorageConnectionEx.a,this);
            }
        }
    }

    //ファイルの書き込み
    private void writeFile(String name,byte[] data) throws Exception {
        String folder;
        StorageConnection c  =null;
        OutputStream      out=null;
        try {
            //フォルダの生成
            folder=name.substring(0,name.lastIndexOf('/')+1);
            c=(StorageConnection)Connector.open(folder);
            if (!c.exists()) c.createFolder();
            c.close();

            //ファイルとの接続
            c=(StorageConnection)Connector.open(name);
            out=c.openOutputStream();

            //ファイルへの書き込み
            out.write(data);

            //ファイルとの切断
            out.close();
            c.close();
        } catch (Exception e) {
            try {
                if (c  !=null) c.close();
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }

    //ファイルの読み込み
    private byte[] readFile(String name) throws Exception {
        int size;
        byte[] w=new byte[10240];
        StorageConnection     c  =null;
        InputStream           in =null;
        ByteArrayOutputStream out=null;
        try {
            //ファイルとの接続
            c  =(StorageConnection)Connector.open(name);
            in =c.openInputStream();
            out=new ByteArrayOutputStream();

            //ファイルからの読み込み
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                out.write(w,0,size);
            }

            //ファイルとの切断
            out.close();
            in.close();
            c.close();
            return out.toByteArray();
        } catch (Exception e) {
            try {
                if (c  !=null) c.close();
                if (in !=null) in.close();
                if (out!=null) out.close();
            } 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 || type==DIALOG_ERROR) {
            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);
        result=-1;
        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 || type==DIALOG_ERROR) {
            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();
    }
}


S!アプリ(P型)のJADファイル
StorageConnectionEx.jad
MIDlet-1: StorageConnectionEx, , StorageConnectionEx
MIDlet-Jar-Size: 3831
MIDlet-Jar-URL: StorageConnectionEx.jar
MIDlet-Name: SConnectionEx
MIDlet-Vendor: My Vendor
MIDlet-Version: 1.0
MicroEdition-Profile: MIDP-1.0
MicroEdition-Configuration: CLDC-1.0
MIDlet-Application-Range: 0,0
MIDlet-OCL: JSCL-1.2.2
MIDlet-Application-Security: Y
MIDlet-Copyright: Y



3GCの制限




−戻る−