import java.io.IOException; import java.util.Enumeration; import javax.microedition.io.Connection; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import javax.microedition.io.file.FileSystemRegistry; import javax.microedition.lcdui.*; import javax.microedition.midlet.MIDlet; public class FileBrowser extends MIDlet implements CommandListener { private List browser; private Display display; private Command exit; private String currDirName; private FileConnection fc; private Enumeration enumeration; public FileBrowser() { currDirName = "/"; } public void startApp() { browser = new List("FileBrowser", 3); exit = new Command("Exit", 7, 3); browser.addCommand(exit); browser.setCommandListener(this); showCurrDir(); display = Display.getDisplay(this); display.setCurrent(browser); } public void pauseApp() { } public void destroyApp(boolean flag) { } public void commandAction(Command command, Displayable displayable) { if(command == List.SELECT_COMMAND) { int i = browser.getSelectedIndex(); String s = browser.getString(i); traverseDirectory(s); showCurrDir(); } if(command == exit) notifyDestroyed(); } private void showCurrDir() { try { browser.deleteAll(); if("/".equals(currDirName)) { enumeration = FileSystemRegistry.listRoots(); } else { String s = "file:///" + currDirName; fc = (FileConnection)Connector.open(s); enumeration = fc.list(); browser.append("..", null); } browser.setTitle(currDirName); String s1; for(;enumeration.hasMoreElements(); browser.append(s1, null)); s1 = (String)enumeration.nextElement(); if(fc != null) fc.close(); } catch(IOException ioexception) { } } private void traverseDirectory(String s) { if(currDirName.equals("/")) currDirName = s; else if(s.equals("..")) { int i = currDirName.lastIndexOf('/', currDirName.length() - 2); if(i != -1) currDirName = currDirName.substring(0, i + 1); else currDirName = "/"; } else { currDirName = currDirName + s; } } }