▼MIDP2.0メモ▼
端末内のファイルやディレクトリにアクセスする


端末内のファイルやディレクトリにアクセスするプログラム。



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

//端末内のファイルやディレクトリにアクセスする(本体)
public class FileConnectionEx extends MIDlet {
    static MIDlet midlet;

    //コンストラクタ
    public FileConnectionEx() {
        midlet=this;
        Display.getDisplay(this).setCurrent(new FileConnectionForm());
    }

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

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

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


FileConnectCanvas.java
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.*;

//端末内のファイルやディレクトリにアクセスする(フォーム)
class FileConnectionForm extends Form
    implements CommandListener,ItemCommandListener,Runnable {
    String  path;               //パス
    String  nextPath="file:///";//次パス
    TextBox textBox;            //テキストボックス

    //コンストラクタ
    public FileConnectionForm(){
        super("");

        //テキストボックス
        textBox=new TextBox("","",1024*10,TextField.ANY);
        textBox.addCommand(new Command("閉",Command.SCREEN,0));
        textBox.setCommandListener(this);
        setCommandListener(this);

        //ディレクトリを開く
        openDir();
    }

    //スレッド
    public void run() {
        if (nextPath.endsWith("/")) {
            openDir();
        } else {
            openText();
        }
    }

    //ディレクトリを開く
    void openDir() {
        Enumeration enm=null;

        //ルート
        if (nextPath.equals("file:///")) {
            enm=FileSystemRegistry.listRoots();
        }
        //ディレクトリ
        else {
            FileConnection fc=null;
            try {
                fc=(FileConnection)
                    Connector.open(nextPath,Connector.READ);
                enm=fc.list();
            } catch (Exception e) {
                return;
            }
        }
        //アイテムの更新
        setTitle(nextPath);
        deleteAll();
        while (enm.hasMoreElements()) {
            addItem((String)enm.nextElement());
        }
        if (!nextPath.equals("file:///")) addItem("..");
        path=nextPath;
    }

    //テキストファイルを開く
    void openText() {
        FileConnection fc=null;
        InputStream    in=null;
        try {
            //テキストファイルを開く
            fc=(FileConnection)
                Connector.open(nextPath,Connector.READ);
            in=fc.openInputStream();
            byte[] w=new byte[1024*10];
            int size=in.read(w);
            in.close();

            //テキストボックスを開く
            textBox.setTitle(nextPath);
            if (size<0) size=0;
            textBox.setString(new String(w,0,size));
            Display.getDisplay(FileConnectionEx.midlet).setCurrent(textBox);
        } catch (Exception e) {
            try {
                if (in!=null) in.close();
            } catch (Exception e2) {
            }
        }
    }

    //アイテムの追加
    void addItem(String text) {
        StringItem item=new StringItem("",text);
        item.setLayout(Item.LAYOUT_2|Item.LAYOUT_NEWLINE_AFTER);
        item.setDefaultCommand(new Command("",Command.SCREEN,0));
        item.setItemCommandListener(this);
        append(item);
    }

    //アイテムイベント
    public void commandAction(Command c,Item item)  {
        String dir=((StringItem)item).getText();
        //親ディレクトリへ移動
        if (dir.equals("..")) {
            int pos=path.lastIndexOf('/',path.length()-2);
            nextPath=path.substring(0,pos+1);
        }
        //子ディレクトリへ移動・テキストファイルを開く
        else if (dir.endsWith("/") || dir.endsWith(".txt")) {
            nextPath=path+dir;
        }
        //その他のファイル
        else {
            return;
        }
        (new Thread(this)).start();
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        int pos=path.lastIndexOf('/',path.length()-2);
        path=path.substring(0,pos+1);
        Display.getDisplay(FileConnectionEx.midlet).setCurrent(this);
    }
}



−戻る−