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

📄 fileexplorer.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * FileExplorer.java
 *
 * cmdCreateed on 2005年3月18日, 下午11:21
 */

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

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

/**
 * 本程序实现了一个简单的文件浏览器,主要具有以下功能:
 * <p>1、文件目录浏览
 * <p>2、新建目录或者文件
 * <p>3、删除目录或者文件
 * <p>4、复制文件
 * <p>5、剪切文件
 * <p>6、粘贴文件
 * <p>7、访问目录或者文件的属性
 * <p>8、访问文件的内容
 * @author Liu Bin
 */
public class FileExplorer implements CommandListener{
    //保存MIDlet实例
    static FileExplorerDemo feInstance = null;
    //保存MIDet的Display
    private Display display = null;
    
    private String currDirName;
    
    private Command cmdView = new Command("查看", Command.ITEM, 1);
    private Command cmdCreate = new Command("新建", Command.ITEM, 2);
    private Command cmdDelete = new Command("删除", Command.ITEM, 3);
    private Command cmdCopy = new Command("复制", Command.ITEM, 4);
    private Command cmdPaste = new Command("粘贴", Command.ITEM, 5);
    private Command cmdCut = new Command("剪切", Command.ITEM, 5);
    private Command cmdProperty = new Command("属性", Command.ITEM, 2);
    private Command cmdCreateOK = new Command("确定", Command.OK, 1);
    private Command cmdBack = new Command("返回", Command.BACK, 2);
    private Command cmdExit = new Command("退出", Command.EXIT, 3);
    
    //创建文件或者目录时,用于输入文件或者目录的名字
    private TextField   nameInput;
    //在创建文件或者目录时,用于选择是创建文件还是创建目录
    private ChoiceGroup typeInput;
    
    private final static String[] attrList = { "读", "写", "隐藏" };
    private final static String[] typeList = { "文件", "目录" };
    
    
    /** 文件和目录的图标 */
    private Image dirIcon, fileIcon;
    private Image[] iconList;
    
    /** 表示父目录的字符串 */
    private final static String FATHER_FOLDER = "..";
    
    /**
     * 表示根目录的字符串
     */
    private final static String ROOT = "/";
    
    /** 规范中定义的,文件夹结尾的字符串 */
    private final static String FOLDER_END_STR = "/";
    
    /** 规范中定义的,文件夹结尾的字符 */
    private final static char   FOLDER_END_CHAR = '/';
    
    /** 可以访问的文件最大长度 */
    public final static int MAX_FILE_LENGTH = 4096;
    
    /** 下面的三个变量用于复制和移动文件 */
    private String clipboardFileName=null;
    private String clipboardDir=null;
    private boolean cutFlag = false;
    
    /** 创建一个文件浏览器实例 */
    public FileExplorer(FileExplorerDemo instance) {
        feInstance = instance;
        display = Display.getDisplay(feInstance);
        
        //缺省的路径为根目录
        currDirName = ROOT;
        
        //装载文件和目录图标
        try {
            dirIcon = Image.createImage("/folder.png");
        } catch (IOException e) {
            dirIcon = null;
        }
        try {
            fileIcon = Image.createImage("/file.png");
        } catch (IOException e) {
            fileIcon = null;
        }
        
        //设置ChoiceGroup的图标
        iconList = new Image[] { fileIcon, dirIcon };
    }
    
    /**
     * 必须执行该方法后,文件浏览器才开始运行
     */
    public void open() {
        try {
            showCurrDir();      //显示目录和文件
        } catch (SecurityException e) {
            //如果有安全限制,则执行下面的代码
            
            Alert alert = new Alert("错误",
                    "您没有权限访问这个受限制的API",
                    null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            Form form = new Form("不能访问FileConnection");
            form.append(new StringItem(null,
                    "以当前的权限,MIDlet不能够访问受限制的API,请给MIDlet签名"
                    + "或者运行在其他的安全域上"));
            form.addCommand(cmdExit);
            form.setCommandListener(this);
            Display.getDisplay(feInstance).setCurrent(alert, form);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    /**
     * 显示当前目录中的文件和目录列表
     */
    void showCurrDir() {
        Enumeration e;
        FileConnection currDir = null;
        List browser;
        try {
            if (currDirName.equals(ROOT)) {
                e = FileSystemRegistry.listRoots();
                browser = new List(currDirName, List.IMPLICIT);
            } else {
                currDir = (FileConnection)Connector.open("file://localhost/" +
                        currDirName);
                e = currDir.list();
                browser = new List(currDirName, List.IMPLICIT);
                
                //如果不是根目录,就添加父目录
                browser.append(FATHER_FOLDER, dirIcon);
            }
            
            while (e.hasMoreElements()) {
                String fileName = (String)e.nextElement();
                if (fileName.charAt(fileName.length()-1) == FOLDER_END_CHAR) {
                    //增加目录
                    browser.append(fileName, dirIcon);
                } else {
                    //增加文件
                    browser.append(fileName, fileIcon);
                }
            }
            
            browser.setSelectCommand(cmdView);
            //根目录不能添加下面这些命令按钮
            if (!ROOT.equals(currDirName)) {
                browser.addCommand(cmdCreate);
                browser.addCommand(cmdProperty);
                browser.addCommand(cmdDelete);
                browser.addCommand(cmdCopy);
                browser.addCommand(cmdCut);
                browser.addCommand(cmdPaste);
            }
            browser.addCommand(cmdExit);
            browser.setCommandListener(this);
            
            if (currDir != null) {
                currDir.close();
            }
            
            display.setCurrent(browser);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    
    /**
     * 进入其他目录
     * <p>
     * @param fileName 要进入的目录
     */
    void traverseDirectory(String fileName) {
        if (currDirName.equals(ROOT)) {
            if (fileName.equals(FATHER_FOLDER)) {
                //确保根目录为顶级目录
                return;
            }
            currDirName = fileName;
        } else if (fileName.equals(FATHER_FOLDER)) {
            //返回上级目录
            int i = currDirName.lastIndexOf(FOLDER_END_CHAR,
                    currDirName.length()-2);
            if (i != -1) {
                //获得父ss目录名字
                currDirName = currDirName.substring(0, i+1);
            } else {
                currDirName = ROOT;            //根目录
            }
        } else {
            currDirName = currDirName + fileName;
        }
        showCurrDir();
    }
    
    /**
     * 在创建一个文件或者文件夹时,显示一个窗口用于输入名字和选择类型
     */
    void showCreateFileForm() {
        Form frmCreateor = new Form("创建文件/文件夹");
        nameInput = new TextField("请输入文件名", null, 256, TextField.ANY);
        typeInput = new ChoiceGroup("请选择文件类型", Choice.EXCLUSIVE,
                typeList, iconList);
        frmCreateor.append(nameInput);
        frmCreateor.append(typeInput);
        frmCreateor.addCommand(cmdCreateOK);
        frmCreateor.addCommand(cmdBack);
        frmCreateor.addCommand(cmdExit);
        frmCreateor.setCommandListener(this);
        display.setCurrent(frmCreateor);
    }
    
    
    
    /**
     * 显示选择的文件内容
     * <p>
     * @param fileName 要显示的文件名
     */
    void showFile(String fileName) {
        try {
            FileConnection fc = (FileConnection)
            Connector.open("file://localhost/" + currDirName + fileName);
            if (!fc.exists()) {
                throw new IOException("文件不存在");
            }
            
            InputStream fis = fc.openInputStream();
            byte[] b = new byte[MAX_FILE_LENGTH];
            
            int length = fis.read(b, 0, MAX_FILE_LENGTH);
            
            fis.close();
            fc.close();
            
            TextBox tbViewer = new TextBox("查看文件: " + fileName,
                    null, MAX_FILE_LENGTH,
                    TextField.ANY | TextField.UNEDITABLE);
            
            tbViewer.addCommand(cmdBack);
            tbViewer.addCommand(cmdExit);
            tbViewer.setCommandListener(this);
            
            if (length > 0) {
                tbViewer.setString(new String(b, 0, length));
            }
            
            display.setCurrent(tbViewer);
        } catch (Exception e) {
            Alert alert = new Alert("错误!",
                    "不能访问目录 " + currDirName +
                    " 中的文件 " + fileName +
                    "\n发生异常:" + e.toString(),
                    null,
                    AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        
    }
    
    /**
     * 显示目录或者文件的属性
     * <p>
     * @param fileName 要查看属性的文件或者目录
     */
    void showPropertyerties(String fileName) {
        try {
            if (fileName.equals(FATHER_FOLDER)) {
                return;
            }
            
            FileConnection fc = (FileConnection)Connector.open(
                    "file://localhost/" + currDirName + fileName);
            
            if (!fc.exists()) {
                throw new IOException("文件不存在");
            }
            
            Form frmPropertys = new Form("cmdPropertyerties: " + fileName);
            ChoiceGroup attrs = new ChoiceGroup("属性:", Choice.MULTIPLE,
                    attrList, null);
            
            attrs.setSelectedFlags(new boolean[] {fc.canRead(),
                    fc.canWrite(),
                    fc.isHidden()}
            );
            
            frmPropertys.append(new StringItem("位置:", currDirName));
            frmPropertys.append(new StringItem("类型:",
                    fc.isDirectory() ? "文件夹": "文件"));
            frmPropertys.append(new StringItem("文件大小:",
                    String.valueOf(fc.fileSize())));
            frmPropertys.append(new StringItem("修改时间:",
                    formatTime(fc.lastModified())));
            frmPropertys.append(attrs);
            
            frmPropertys.addCommand(cmdBack);
            frmPropertys.addCommand(cmdExit);
            frmPropertys.setCommandListener(this);
            
            fc.close();
            

⌨️ 快捷键说明

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