麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發(fā)設計 > 正文

使用FileConnection訪問文件系統(tǒng)

2019-11-18 16:17:24
字體:
來源:轉載
供稿:網友

The FileConnection API specified in JSR-75 gives access to the local file systems on devices like PDA. In order to overcome security issues MIDlet needs to include requested file permission in its JAD file under MIDLet-Permission PRoperty.The example below has a file browser to move around the file system, select and read images stored in its local image Directory.

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("View", Command.ITEM, 1);
  private Command back = new Command("Back", Command.BACK, 2);
  private Command exit = new Command("Exit", 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)) 
      {
        // can not go up from MEGA_ROOT
        return;
      }
      currDirName = fileName;
    } 
    else if (fileName.equals(UP_DIRECTORY)) 
    {
      // Go up one directory
      // TODO use setFileConnection when implemented
      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("File does not exists");
      }
      InputStream fis = fc.openInputStream();
      byte[] b = new byte[1024];
      int length = fis.read(b, 0, 1024);
      fis.close();
      fc.close();

      TextBox tb = new TextBox("View File: " + 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) {}
  }
}


(出處:http://www.companysz.com)



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 激情久久精品 | 最新中文字幕第一页视频 | 超久久 | 国产精品久久久久久久久粉嫩 | 欧美性猛交xxx乱大交3蜜桃 | 精品久久久久99 | 久久久久久久久久久久久久av | 精品国产一二区 | 日韩精品久久久久久久九岛 | 午夜视频在线观看免费视频 | 羞羞答答视频 | 亚洲精品91| 正在播放91视频 | 欧美18—19sex性hd | 狠狠干视频网站 | 免费看成年人视频在线 | 91av原创| 亚洲午夜久久久精品一区二区三区 | sesee99| a黄色网| 五月天影院,久久综合, | 视频毛片 | 国产美女视频一区二区三区 | 国产精品成aⅴ人片在线观看 | 九九热精品在线视频 | 久久精品亚洲欧美日韩精品中文字幕 | 黑人一区二区三区四区五区 | 国产精品色综合 | 一级毛片播放 | av不卡毛片 | 国产亚洲精品久久久久久久软件 | 国产91精品亚洲精品日韩已满 | 91精品国产91久久久久久吃药 | 成人毛片免费看 | 中文字幕xxx | 免费国产之a视频 | 黄色免费不卡视频 | 久久免费视频精品 | 国产精品一区二区三区99 | 极色品影院 | 久精品久久 |