⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filebrowser.java

📁 Java手机文件浏览器 索爱K750c测试成功
💻 JAVA
字号:
 /*
 * FileBrowser.java
 *
 * Created on 2007年10月18日
 */



import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class FileBrowser extends MIDlet implements CommandListener
{
    public String currentDir = ""; 

    private Display dspFileBrowse; 
    
    private Command exitCommand; 
    private Command openCommand; 
    
    private List fileList;
    
    private String fathorFolder = "..";
    

    public FileBrowser()
    {
        this.dspFileBrowse = Display.getDisplay(this);
        
        this.fileList = new List("文件浏览器", List.IMPLICIT);
        
        this.exitCommand = new Command("退出", Command.EXIT, 1);
        this.openCommand = new Command("打开", Command.OK, 1);
        

        this.fileList.addCommand(this.openCommand);
        this.fileList.addCommand(this.exitCommand);
        this.fileList.setCommandListener(this);

        this.dspFileBrowse.setCurrent(this.fileList); 
    }
    
    public void startApp()
    {
        this.getRoots();
    }
    
    public void pauseApp()
    {
    }
    
    public void destroyApp(boolean unconditional)
    {
        this.notifyDestroyed();
    }

    public void commandAction(Command command, Displayable displayable)
    {
        if(command == this.openCommand)
        {
            this.judgeDir();
        }
        if(command == this.exitCommand)
        {
            this.destroyApp(true);
        }
    }
    

    private void judgeDir()
    {
        String currSelect = getCurrentSelect();
   
        if(currSelect == "..")
        {
            int loopTime = this.currentDir.length() - 2;
            char[] charTemp = this.currentDir.toCharArray();
            char folder ='/';
            while(loopTime > 0)
            {
                if(charTemp[loopTime] == folder)
                {
                    break;
                }
                loopTime --;
            }
            if(loopTime == 0)
            {
                this.getRoots();
            }
            else
            {
                this.currentDir = this.currentDir.substring(0, loopTime+1);
                this.getFolders(this.currentDir);
            }
            return;
        }

        int strLen = currSelect.length();
        String lastStr = currSelect.substring(strLen-1, strLen);
        if(lastStr.equals("/"))
        {
            this.currentDir += currSelect;
            this.getFolders(this.currentDir);
            return;
        }
        return;
    }
    

    private void getRoots()
    {
        this.currentDir = "";
        Enumeration enumRoot = FileSystemRegistry.listRoots();
        this.fileList.deleteAll();
        while(enumRoot.hasMoreElements())
        {
            this.fileList.append((String)enumRoot.nextElement(), null);
        }
    }
    

    private String getCurrentSelect()
    {
        int a = this.fileList.getSelectedIndex();
        String result = this.fileList.getString(a);
        return result;
    }
    
    private void getFolders(String dir)
    {
        try
        {
            FileConnection fc = (FileConnection) Connector.open("file://localhost/" + dir);
            Enumeration enumFolders = fc.list();
            this.fileList.deleteAll();
            this.fileList.append(this.fathorFolder, null);
            while(enumFolders.hasMoreElements())
            {
                String listinfo = (String)enumFolders.nextElement();
                this.fileList.append(listinfo, null);
            }
        } catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -