Acessando o sistema de arquivos local em dispositivos J2ME com a API FileConnection

A API FileConnection especificada em Jsr-75 dá acesso aos sistemas de arquivo locais em dispositivos como PDAs. O exemplo abaixo tem um browser para se movimentar no sistema de arquivos, permitindo selecionar e ler imagens armazenadas no diretório local.

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

public class FileMidlet extends MIDlet implements CommandListener 
{
  private String currDirName;

  private Command view = new Command("Visualizar", Command.ITEM, 1);
  private Command back = new Command("Voltar", Command.BACK, 2);
  private Command exit = new Command("Sair", Command.EXIT, 3);
  
  private final static String UP_DIRECTORY = "..";
  private final static String MEGA_ROOT = "/";
  private final static String SEP_STR = "/";
  private final static char   SEP = '/';

  public FileMidlet() 
  {
    currDirName = MEGA_ROOT;
  }

  public void startApp() 
  {
    boolean isAPIAvailable = false;
    if (System.getProperty(
      "microedition.io.file.FileConnection.version") != null)
    {
      isAPIAvailable = true;
      try 
      {
        showCurrDir();
      
      catch (SecurityException e) 
      {} 
      catch (Exception e) {}
      }
    else
    {
      StringBuffer splashText = new StringBuffer(
        getAppProperty("MIDlet-Name")).append("\n").append(
        getAppProperty("MIDlet-Vendor")).
        append(isAPIAvailable?"":"\nFileConnection API not available");
      Alert splashScreen = new Alert(null,splashText.toString(),
        null,AlertType.INFO);
      splashScreen.setTimeout(3000);
      Display.getDisplay(this).setCurrent(splashScreen);
    }
    
  }

  public void pauseApp() {}

  public void destroyApp(boolean cond) 
  {
    notifyDestroyed();
  }

  public void commandAction(Command c, Displayable d) 
  {
    if (c == view) 
    {
      List curr = (List)d;
      final String currFile = curr.getString(curr.getSelectedIndex());
      new Thread(new Runnable() 
      {
        public void run() 
        {
          if (currFile.endsWith(SEP_STR) || 
            currFile.equals(UP_DIRECTORY)) 
          {
            traverseDirectory(currFile);
          else 
          {
            showFile(currFile);
          }
        }
      }).start()
    }
    else if (c == back) 
    {
      showCurrDir();
    
    else if (c == exit) 
    {
      destroyApp(false);
    }
  }

  void showCurrDir() 
  {
    Enumeration e;
    FileConnection currDir = null;
    List browser;
    try 
    {
      if (MEGA_ROOT.equals(currDirName)) 
      {
        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(UP_DIRECTORY);
      }
      while (e.hasMoreElements()) 
      {
        String fileName = (String)e.nextElement();
        if (fileName.charAt(fileName.length()-1) == SEP) 
        {
          browser.append(fileName);
        
        else 
        {
          browser.append(fileName);
        }
      }
      browser.setSelectCommand(view);
      browser.addCommand(exit);
      browser.setCommandListener(this);
      if (currDir != null
      {
        currDir.close();
      }
      Display.getDisplay(this).setCurrent(browser);
    
    catch (IOException ioe) 
    {}
  }
  
  void traverseDirectory(String fileName) 
  {
    if (currDirName.equals(MEGA_ROOT)) 
    {
      if (fileName.equals(UP_DIRECTORY)) 
      {
        return;
      }
      currDirName = fileName;
    
    else if (fileName.equals(UP_DIRECTORY)) 
    {
      int i = currDirName.lastIndexOf(SEP, currDirName.length()-2);
      if (i != -1
      {
        currDirName = currDirName.substring(0, i+1);
      
      else 
      {
        currDirName = MEGA_ROOT;
      }
    
    else 
    {
      currDirName = currDirName + fileName;
    }
    showCurrDir();
  }
  
  void showFile(String fileName) 
  {
    try 
    {
      FileConnection fc = (FileConnection)
      Connector.open("file://localhost/" + currDirName + fileName);
      if (!fc.exists()) 
      {
        throw new IOException("Arquivo não existe");
      }
      InputStream fis = fc.openInputStream();
      byte[] b = new byte[1024];
      int length = fis.read(b, 01024);
      fis.close();
      fc.close();

      TextBox tb = new TextBox("Visualizar arquivo: " + fileName, null, 1024,
          TextField.ANY | TextField.UNEDITABLE);
  
      tb.addCommand(back);
      tb.addCommand(exit);
      tb.setCommandListener(this);

      if (length > 0
      {
        tb.setString(new String(b, 0, length));
      }
      Display.getDisplay(this).setCurrent(tb);
    }
    catch (Exception e) {}
  }
}