一个手机文件浏览器,比较实用,记录一下,需要jsr75的支持。 package demo; import java.io.IOException; import java.util.Enumeration; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.FileSystemRegistry; import javax.microedition.lcdui.Choice; import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.List; import javax.microedition.midlet.MIDlet; import com.hiyo.cx.tool.Tools; 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("手机手机文件浏览器", Choice.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); // Test System.out.println("create a file"); Tools.createAFile("1.txt", "12345"); System.out.println("create a file ok"); } 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; } // 选中的是文件不做任何操作 System.out.println("currSelect=" + currSelect); 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) { // System.out.println(dir); try { FileConnection fc = (FileConnection) Connector .open("file://localhost/" + dir);// 创建对应的连接对象 Enumeration enumFolders = fc.list();// 调用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(); } } } 若用模拟器运行时,看不到任何文件,于是贴一个创建文件的方法: // Create a file. public static void createAFile(String fn, String str) { FileConnection fc = null; try { fc = (FileConnection) Connector.open("file://localhost/" + getRoot() + "/" + fn); if (!fc.exists()) fc.create(); OutputStream os = fc.openOutputStream(); byte[] res = str.getBytes(); os.write(res, 0, res.length); os.close(); fc.close(); } catch (IOException ex) { ex.printStackTrace(); } } // Create a file. public static void createAFolder(String fn) { FileConnection fc = null; try { fc = (FileConnection) Connector.open("file://localhost/" + getRoot() + "/" + fn); if (!fc.isDirectory()) fc.mkdir(); fc.close(); } catch (IOException ex) { ex.printStackTrace(); } }