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

📄 fileexplorer.java

📁 用J2ME开发的文件浏览器
💻 JAVA
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. *//** * * @author Administrator */package hello;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import java.util.*;import java.io.*;import javax.microedition.io.*;import javax.microedition.io.file.*;public class FileExplorer implements CommandListener{        static Myfile fileinstance;    private Display display=null;    private TextField nameInput;    private ChoiceGroup typeInput;                        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 cmdBack=new Command("返回",Command.BACK,2);    private Command cmdExit=new Command("退出",Command.EXIT,3);    private Command cmdCreateOk=new Command("确定",Command.OK,1);            private final static String ROOT="/";    private Image dirIcon,fileIcon;    private Image[] iconList;        private final static String FATHER_FOLDER="..";        private final static String[]  typeList={"文件","目录"};    private final static String[]  attrList={"read","write","hidden"};                public FileExplorer(Myfile instance)    {        fileinstance=instance;        display=Display.getDisplay(fileinstance);                currDirName=ROOT;                try{            dirIcon=Image.createImage("/folder.png");                    }catch(IOException e)        {            dirIcon=null;        }                try{           fileIcon=Image.createImage("/file.png");                    }catch(IOException e)        {            fileIcon=null;        }                        iconList=new Image[]{fileIcon,dirIcon};                    }        public void open()    {                try {            showCurrdir();        }catch(SecurityException e)        {            Alert alert=new Alert("Error","NO Pemission",null,AlertType.ERROR);            alert.setTimeout(Alert.FOREVER);            Form form=new Form("Cannot access this folder");            form.append(new StringItem(null,"no security"));            form.addCommand(cmdExit);            form.setCommandListener(this);            Display.getDisplay(fileinstance).setCurrent(alert,form);                    }            }        public 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)=='/')                    browser.append(fileName, dirIcon);                else                    browser.append(fileName, fileIcon);                                        }                                    browser.addCommand(cmdView);            if(!ROOT.equals(currDirName)){                browser.addCommand(cmdCreate);                 browser.addCommand(cmdProperty);                  browser.addCommand(cmdDelete);                   browser.addCommand(cmdCopy);                    browser.addCommand(cmdCut);                     browser.addCommand(cmdPaste);                            }                        browser.setSelectCommand(cmdExit);            browser.setCommandListener(this);            if(currDir!=null)                currDir.close();            display.setCurrent(browser);                    }catch(IOException ioe){            ioe.printStackTrace();        }            }        public void traverse(String file){        if(currDirName.equals(ROOT)){            if(file.equals(".."))                return ;            currDirName=file;        }        else if(file.equals("..")){            int i=currDirName.lastIndexOf('/',currDirName.length()-2);            if(i!=-1)                currDirName=currDirName.substring(0,i+1);            else currDirName=ROOT;        }        else currDirName=currDirName+file;        showCurrdir();            }        public void showFile(String fileName){                try{            FileConnection fc=(FileConnection)Connector.open("file://localhost/"+currDirName+fileName);            if(!fc.exists())                throw new IOException("The file not exits");                        InputStream fis=fc.openInputStream();            byte []b=new byte[4096];                        int length=fis.read(b,0,4096);            fis.close();            fc.close();                        TextBox tv=new TextBox("This is the File:"+fileName,null,4096,TextField.ANY|TextField.UNEDITABLE);            tv.addCommand(cmdBack);            tv.addCommand(cmdExit);            tv.setCommandListener(this);            if(length>0)                tv.setString(new String(b,0,4096));                    }catch(Exception e){            Alert alert=new Alert("ERROR",fileName,null,AlertType.ERROR);            display.setCurrent(alert);                    }            }        public void showCreateFile(){        Form frm=new Form("创建文件夹/文件");        nameInput=new TextField("请输入文件名",null,256,TextField.ANY);        typeInput=new ChoiceGroup("请选择文件类型",Choice.EXCLUSIVE,typeList,iconList);        frm.append(nameInput);        frm.append(typeInput);        frm.addCommand(cmdCreateOk);        frm.addCommand(cmdExit);        frm.setCommandListener(this);        display.setCurrent(frm);         }        public void createFile(String name,boolean isD){        try{            FileConnection fc=(FileConnection)Connector.open("file:///"+currDirName+name);            if(isD)                fc.mkdir();            else fc.create();                    }catch(Exception ee){             Alert alert=new Alert("Not null","cannot create file",null,AlertType.ERROR);             alert.setTimeout(Alert.FOREVER);             display.setCurrent(alert);                           Display.getDisplay(fileinstance).getCurrent().removeCommand(cmdCreateOk);              Display.getDisplay(fileinstance).getCurrent().removeCommand(cmdBack);                    }            }    public void execute(final String name,final boolean val){         new Thread(new Runnable(){                    public void run(){                        createFile(name,val);                    }         }).start();    }        public void delete(String filename){        try{            if(filename.equals(".."))                return ;            FileConnection fc=(FileConnection)Connector.open("file://localhost/"+currDirName+filename);            if(!fc.exists())                throw new IOException("The file doesnot exits");            if(fc.isDirectory())                throw new IOException("cannot delete a dir");                        fc.delete();                    }catch(Exception de){             Alert alert=new Alert("Not null","cannot delete file",null,AlertType.ERROR);             alert.setTimeout(Alert.FOREVER);             display.setCurrent(alert);                    }    }        public void showProperty(String filename){          try{            if(filename.equals(".."))                return ;            FileConnection fc=(FileConnection)Connector.open("file://localhost/"+currDirName+filename);            if(!fc.exists())                throw new IOException("The file doesnot exits");            if(fc.isDirectory())                throw new IOException("cannot delete a dir");                       Form fm=new Form("cmdProperty"+filename);            ChoiceGroup attrs=new ChoiceGroup("Attri",Choice.MULTIPLE,attrList,null);            attrs.setSelectedFlags(new boolean[]{fc.canRead(),fc.canWrite(),fc.isHidden()});                        fm.append(new StringItem("Position:",currDirName));            fm.append(new StringItem("Type:",fc.isDirectory()?"Dir":"File"));            fm.append(new StringItem("Size:",String.valueOf(fc.fileSize())));            fm.append(attrs);                        fm.addCommand(cmdBack);            fm.addCommand(cmdExit);            fm.setCommandListener(this);                        fc.close();            display.setCurrent(fm);                      }catch(Exception de){                         Alert alert=new Alert("Not null","cannot show property",null,AlertType.ERROR);             alert.setTimeout(Alert.FOREVER);             display.setCurrent(alert);                    }            }        public void copy(String filename){         try{            if(filename.equals(".."))                return ;            FileConnection fc=(FileConnection)Connector.open("file://localhost/"+currDirName+filename);            if(!fc.exists())                throw new IOException("The file doesnot exits");            if(fc.isDirectory())                throw new IOException("cannot delete a dir");                        this.clipFile=filename;            this.clipDir=currDirName;            fc.close();         }catch(Exception ce){                          Alert alert=new Alert("Not null","cannot copy file",null,AlertType.ERROR);             alert.setTimeout(Alert.FOREVER);             display.setCurrent(alert);                      }            }        public void paste(){        String filename=this.clipFile;           try{            if(filename.equals(".."))                return ;            FileConnection fs=(FileConnection)Connector.open("file://localhost/"+this.clipDir+filename);            FileConnection fd=(FileConnection)Connector.open("file://localhost/"+currDirName+filename);            if(!fs.exists())                throw new IOException("The file doesnot exits");            if(fs.isDirectory())                throw new IOException("cannot delete a dir");            fs.create();            InputStream is=fs.openInputStream();            OutputStream os=fd.openOutputStream();            byte []buf=new byte[4096];            is.read(buf);            os.write(buf);            os.flush();            this.clipDir=null;            this.clipFile=null;            this.cutflag=false;            showCurrdir();                    }catch(Exception ce){                          Alert alert=new Alert("Not null","cannot paste file",null,AlertType.ERROR);             alert.setTimeout(Alert.FOREVER);             display.setCurrent(alert);                      }                            }            public String clipFile;    public String clipDir;    public boolean cutflag=false;        public void commandAction(Command c,Displayable s){                if(c==cmdView){            List curr=(List)s;            final String currFile=curr.getString(curr.getSelectedIndex());            new Thread(new Runnable(){                public void run(){                    if(currFile.endsWith("/"))                            traverse(currFile);                    else if(currFile=="..")                            traverse(currFile);                    else showFile(currFile);                                    }            }).start();                    }        else if(c==cmdCreate){            showCreateFile();        }        else if(c==cmdCreateOk){            final String newName=nameInput.getString();            if(newName==null || newName.equals("")){                  Alert alert=new Alert("Not null","input the filename",null,AlertType.ERROR);                  alert.setTimeout(Alert.FOREVER);                  display.setCurrent(alert);                            }else {               execute(newName,typeInput.getSelectedIndex()!=0);                                Display.getDisplay(fileinstance).getCurrent().removeCommand(cmdCreateOk);                Display.getDisplay(fileinstance).getCurrent().removeCommand(cmdBack);                                            }             }        else if(c==cmdDelete){            final List curr=(List)s;            new Thread(new Runnable(){                public void run(){                    delete(curr.getString(curr.getSelectedIndex()));                }            }).start();        }        else if(c==cmdProperty){            List curr=(List)s;            String currFile=curr.getString(curr.getSelectedIndex());            showProperty(currFile);        }        else if(c==cmdCopy){            final List curr=(List)s;            new Thread(new Runnable(){                public void run(){                    copy(curr.getString(curr.getSelectedIndex()));                }            }).start();                    }        else if(c==cmdCut){            cutflag=true;             final List curr=(List)s;            new Thread(new Runnable(){                public void run(){                    copy(curr.getString(curr.getSelectedIndex()));                }            }).start();                    }        else if(c==cmdPaste){            if(this.clipFile!=null && this.clipDir!=null){                  new Thread(new Runnable(){                public void run(){                    paste();                }            }).start();                      }        }        else if(c==cmdBack){            showCurrdir();                            }        else if(c==cmdExit){            showCurrdir();        }            }}

⌨️ 快捷键说明

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