`
ming_fanglin
  • 浏览: 223580 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

j2me 本地文件访问遍历

阅读更多

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());
      System.out.println(currFile);
      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,null);
    
      }
      while (e.hasMoreElements())
      {
        String fileName = (String)e.nextElement();
        if (fileName.charAt(fileName.length()-1) == SEP)
        {
          browser.append(fileName,null);
        }
        else
        {
          browser.append(fileName,null);
        }
      }
      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) {}
}
}

分享到:
评论

相关推荐

    file_j2me.rar_j2me_j2me 文件_读写文件

    URL 的前缀 "file://" 表示这是一个本地文件,而 "/store0/" 是存储区的标识,"myfile.txt" 是文件名。 读取文件的基本步骤如下: 1. 创建一个 `StreamConnection` 对象,使用 `Connector.open()` 方法,并传入...

    j2me文件浏览器例程

    本文将深入探讨一个基于J2ME的文件浏览器例程——FilebrowserExample,这是一个在NetBeans集成开发环境中创建的例子,用于在移动设备上展示本地文件系统。 **一、J2ME基础知识** 1. ** MIDP (Mobile Information ...

    手机通讯录 j2me 源码

    2. MIDP(Mobile Information Device Profile):这是J2ME中用于开发移动应用的常用profile,它包括用户界面组件(如Canvas和Form)、网络通信支持以及文件系统访问等。 二、手机通讯录功能实现 1. 新建联系人:该...

    J2ME 手机图书馆

    考虑到内存和性能限制,搜索可能需要在本地数据库或索引中进行,而不是每次搜索时都遍历所有文件。 **查看目录**是指在打开TXT文件前预览其内容结构,例如章节标题。对于非结构化的TXT文件,可能需要通过分析行首的...

    local_visit.rar_pim in j2me_pim j2me

    本文将深入探讨如何在J2ME中利用JSR 75包中的PIM API来访问手机本地的电话本。 首先,JSR 75为PIM提供了统一的接口,使得开发者可以跨不同设备和操作系统编写代码。这包括了创建、读取、更新和删除联系人、事件等...

    tengge手机端j2me编程教程.doc

    1. **文件操作**:读取、写入、播放本地文件,甚至从压缩包中提取资源。 2. **音乐播放**:使用MediaManager播放音频,控制音量。 3. **资源管理**:包括文件夹操作和文件操作。 【工具类和基本类的使用】 J2ME...

    J2ME读取电话号码本内容

    在Java 2 Micro Edition (J2ME)平台上,开发人员可以访问特定的手机功能,如读取电话号码本。这个项目就是围绕这个主题展开的,它允许程序读取存储在设备本地以及SIM卡上的联系人信息。下面我们将深入探讨如何在J2ME...

    J2ME个人通讯录(你会看到奇迹)

    在J2ME中,核心概念包括配置(Configurations)和 profiles(配置文件)。配置定义了设备的基本硬件和软件特性,而profile则是在特定配置基础上定义的应用程序接口和功能集。例如,Mobile Information Device ...

    文件操作-JSR75包使用总结

    在实际应用中,JSR75的文件操作功能为开发者提供了在移动设备上处理本地文件的强大工具。它可以用来存储用户数据、读取配置文件、进行数据备份等。然而,由于不同的设备可能会有不同的文件系统限制,所以在编写代码...

    基于J2ME的手机游戏开发定稿

    - **永久性数据(RMS)**: J2ME提供了Record Management System (RMS),用于在设备上存储小型数据文件。 - **存储集(RecordStore)**: RMS中的数据以RecordStore的形式组织。 - **记录**: RecordStore中的单个数据项被...

    j2me RecordStore管理

    RecordStore 管理系统(RMS)是J2ME为小型应用程序提供的一种本地数据存储机制。它允许开发者保存和检索应用程序的数据,即使在设备断电或应用程序关闭后也能保持数据的持久性。以下是对"j2me RecordStore管理"的...

    JAVA上百实例源码以及开源项目源代码

    J2ME优化压缩PNG文件 4个目标文件 内容索引:JAVA源码,综合应用,J2me游戏,PNG,图形处理 这是个J2ME控制台程序,它能剔除PNG文件中的非关键数据段,减少文件大小从而达到压缩图片的目的。而图片的质量并不会受到损失...

    JAVA面试题全集

    - Applet 运行在一个沙箱环境中,限制了其对本地资源的访问。 21. **逻辑操作与条件操作的区别** - 逻辑操作 (`&`, `|`) 会计算两边的操作数。 - 条件操作 (`&&`, `||`) 在某些情况下可以短路计算。 22. **...

    java达内面试题库

    1. Java发展历程:了解Java的起源、版本迭代,如J2SE、J2EE、J2ME的演变。 2. Java特性:理解面向对象编程(OOP)的概念,包括封装、继承、多态。 3. 数据类型与变量:区分基本数据类型与引用数据类型,理解变量的...

    java面试试题

    这可以通过循环遍历字符串并逐个字符反转来实现。 2. **数值类型转换:** - **整型与浮点型之间的转换:** Java 提供了多种方式来进行基本数据类型间的转换,如 Integer.valueOf 和 Double.valueOf 可用于将字符串...

    Java面试题及解惑

    - **字符串反转**:可以通过循环遍历字符串并反向拼接来实现。 - **字符串替换**:可以使用`replace`或`replaceAll`方法进行替换。 #### 3. 数据类型之间的转换 - **数值型字符转换为数字**:可以使用`Integer....

Global site tag (gtag.js) - Google Analytics