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

📄 filemanager.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import java.io.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import java.util.*;

/**
 * 该类提供了管理移动设备文件的功能
 */
public class FileManager {
    public static final String DRIVER_ROOT = "/";
    private static final String URL_HEAD = "file://";
    public static final String PARENT_DIR = "..";
    
    private static String strSep = "/";
    private static char chSep = '/';
    private String currDir;         //当前目录
    private FileConnection fconn;   //指向当前目录的文件连接
    private Vector sysRoots;        //文件系统的所有根目录
    
    //构造方法创建管理器,初始状态当前目录为:DRIVER_ROOT,fconn为:null
    public FileManager() {
        //String strSep = System.getProperty("file.separator");
        //separator = strSep.charAt(0);
        currDir = DRIVER_ROOT;
        sysRoots = createSysRoots();
    }
    
    //获取文件系统所有的根
    private Vector createSysRoots() {
        Enumeration e = null;
        e = FileSystemRegistry.listRoots(); 
        
        Vector v = new Vector();
        String rootName = null;
        while(e.hasMoreElements()) {
            rootName = (String)e.nextElement();
            v.addElement(rootName);
        }
        
        return v;
    }
    
    //获取当前目录下所有文件
    private Vector getCurrDirFiles() throws IOException {
        if(currDir == DRIVER_ROOT) { //当前目录为设备的根目录
            return sysRoots;
        }
        
        //当前目录不是设备的根
        Enumeration e = null;
        e = fconn.list();   //获取当前目录下文件列表
        
        String fileName;
        Vector v = new Vector();
        v.addElement(PARENT_DIR);
        while(e.hasMoreElements()) {
            fileName = (String)e.nextElement();
            v.addElement(fileName);
        }
        
        return v;
    }
    
    //获取当前目录下的所有文件列表
    public String[] list() throws IOException {
        Vector v = getCurrDirFiles();
        
        String[] fileArray = new String[v.size()];
        v.copyInto(fileArray);
        
        return fileArray;
    }
    
    //获取系统的根目录列表
    public String[] getSysRoots() {
        sysRoots = createSysRoots();
        
        String[] r = new String[sysRoots.size()];
        sysRoots.copyInto(r);
        
        return r;
    }
    
    //在当前目录下创建文件
    public void create(File file) throws IOException {
        if(currDir==DRIVER_ROOT) {
            throw new IOException("不能创建一个根目录:" + file);
        }
        
        String fileUrl = URL_HEAD + currDir + file.getName();
        FileConnection fc = (FileConnection)Connector.open(fileUrl);
        if(fc.exists()) {
            throw new IOException("文件已经存在!");
        }
        
        //创建文件,并设置相应的属性
        if(file.isDirectory()) {
            fc.mkdir();
        }
        else {
            fc.create();
        }
        fc.setReadable(file.canRead());
        fc.setWritable(file.canWrite());
        fc.setHidden(file.isHidden());
        
        fc.close();
    }
    
    //删除当前目录中的fileName文件
    public void delete(String fileName) throws IOException {
        if(currDir == DRIVER_ROOT) {
            throw new IOException("不能删除根: " + fileName);
        }
        
        String fileUrl = URL_HEAD + currDir + fileName;
        FileConnection fc = (FileConnection)Connector.open(fileUrl);
        if(!fc.canWrite()) {    //文件不可写
            throw new IOException("文件:" + fileName + "不可写,不能删除!");
        }
        if(fc.isDirectory()) {  //是目录
            Enumeration e = fc.list();
            if(e.hasMoreElements()) {
                throw new IOException("不能删除非空目录:" + fileName);
            }
        }
        
        fc.delete();
        fc.close();
    }
    
    //获取当前目录下文件f的属性
    public File getProperty(String fileName) throws IOException {
        File f = new File(fileName);    //创建文件对象
        String fileUrl = URL_HEAD + currDir + fileName;
        FileConnection fc = (FileConnection)Connector.open(fileUrl);
        f.setReadable(fc.canRead());
        f.setWritable(fc.canWrite());
        f.setHidden(fc.isHidden());
        f.setLastModify(fc.lastModified());
        fc.close();
        
        return f;
    }
    
    //把文件srcFile复制到当前目录中,新文件名为:newFileName
    public void copy(String srcFile, String newFileName) throws IOException {
        String srcFileUrl = URL_HEAD + srcFile;
        System.out.println("src: " + srcFileUrl);
        FileConnection srcFc = (FileConnection)Connector.open(srcFileUrl);
        if(!srcFc.exists() || !srcFc.canRead()) {
            throw new IOException("源文件:\"" + srcFile + "\"不存在,或者不可读!");
        }
        if(srcFc.isDirectory()) {
            throw new IOException("源文件:\"" + srcFile + "\"是目录,不能复制!");
        }
        if(currDir == DRIVER_ROOT) {
            throw new IOException("当前目录是设备的根,不能粘贴文件!");
        }
        
        String desFileUrl = URL_HEAD + currDir + newFileName;
        System.out.println("des: " + desFileUrl);
        FileConnection desFc = (FileConnection)Connector.open(desFileUrl);
        if(desFc.exists()) {   //当前目录中有同名的文件,
            throw new IOException("当前目录中有同名的文件,不能粘贴文件。");
        }
        
        //复制
        desFc.create();    //创建目标文件
        InputStream is = srcFc.openInputStream();
        OutputStream os = desFc.openOutputStream();
        byte[] buf = new byte[200];
        int n = -1;
        while((n = is.read(buf, 0, buf.length)) != -1) {
            os.write(buf, 0, n);
        }
        is.close();
        os.close();
        srcFc.close();
        desFc.close();
    }
    
    //转换到dirName目录。dirName只能是当前目录的父目录(..),或者当前目录的子目录
    public void changeDirectory(String dirName) throws IOException {
        if(dirName == PARENT_DIR) { //要转换到父目录
            if(currDir == DRIVER_ROOT) {    
                //
            }
            else {
                if(FileManager.getPath(currDir).equals(DRIVER_ROOT)) { //当前目录为文件系统的根
                    currDir = DRIVER_ROOT;
                    fconn.close();
                    fconn = null;
                }
                else {
                    fconn.setFileConnection(PARENT_DIR);
                    currDir = FileManager.getPath(currDir);
                }
            }
        }
        else {  //要转换到子目录
            if(currDir == DRIVER_ROOT) {
                currDir = currDir+dirName;
                String dirUrl = URL_HEAD + currDir;
                fconn = (FileConnection)Connector.open(dirUrl);
            }
            else {
                currDir = currDir+dirName;
                fconn.setFileConnection(dirName);
            }
        }
    }
    
    //返回当前目录
    public String getCurrDir() {
        return currDir;
    }
    
    //释放管理器资源
    public void release() {
        try {
            if(fconn != null) {
                fconn.close();
            }
        }
        catch(IOException ioe) {
            
        }
    }
    
    //获取文件名
    public static String getName(String pathName) {
        int index = pathName.lastIndexOf(chSep, pathName.length()-2);
        String name = pathName;
        if(index != -1) {
            name = pathName.substring(index+1);
        }
        
        return name;
    }
    
    public static String getPath(String pathName) {
        int index = pathName.lastIndexOf(chSep, pathName.length()-2);
        String path = "";
        if(index != -1) {
            path = pathName.substring(0, index+1);
        }
        
        return path;
    }
    
    public static boolean isDirectory(String dir) {
        return dir.endsWith(strSep)||dir.endsWith(PARENT_DIR);
    }
    
    //检测当前目录是否为设备的根
    public boolean isDriveRoot() {
        return currDir.equals(DRIVER_ROOT);
    }
    
    //打开文件
    public long read(String file, ByteArrayOutputStream baos) throws IOException {
        String fileUrl = URL_HEAD + currDir + file;
        FileConnection fc = (FileConnection)Connector.open(fileUrl);
        if(!fc.exists()) {
            throw new IOException("文件:\"" + file + "\"不存在!");
        }
        if(fc.isDirectory()) {
            throw new IOException("不能打开目录文件:\n" + file +"\"!");
        }
        if(!fc.canRead()) {
            throw new IOException("文件:\n" + file +"\"不能读!");
        }
        
        InputStream is = fc.openInputStream();
        int n = -1;
        int total = 0;
        byte[] buf = new byte[250];
        while((n = is.read(buf, 0, buf.length)) != -1) {
            baos.write(buf, 0, n);
            total += n;
        }
        
        is.close();
        fc.close();
        
        return total;
    }
}

⌨️ 快捷键说明

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