下面是完整的代码,模拟器测试通过,但是真机没有试,因为没有^_^
/*
* MobileFileBrowser.java
*
* Created on 2006年10月18日, 下午3:34
*/
package hello;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author 冯东
* @version
*/
public class MobileFileBrowser extends MIDlet implements CommandListener
{
public String currentDir = "";//当前路径
private Display dspFileBrowse;//显示管理对象
private Command exitCommand;//退出按钮
private Command openCommand;//打开文件或文件夹按钮
private List fileList;//显示文件夹内容的列表框
private String fathorFolder = "..";
//构造函数
public MobileFileBrowser()
{
this.dspFileBrowse = Display.getDisplay(this);
this.fileList = new List("手机手机文件浏览器", List.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);
}
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;
}
//选中的是文件不做任何操作
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();
}
}
}
* MobileFileBrowser.java
*
* Created on 2006年10月18日, 下午3:34
*/
package hello;
import java.io.IOException;
import java.util.Enumeration;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/**
*
* @author 冯东
* @version
*/
public class MobileFileBrowser extends MIDlet implements CommandListener
{
public String currentDir = "";//当前路径
private Display dspFileBrowse;//显示管理对象
private Command exitCommand;//退出按钮
private Command openCommand;//打开文件或文件夹按钮
private List fileList;//显示文件夹内容的列表框
private String fathorFolder = "..";
//构造函数
public MobileFileBrowser()
{
this.dspFileBrowse = Display.getDisplay(this);
this.fileList = new List("手机手机文件浏览器", List.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);
}
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;
}
//选中的是文件不做任何操作
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();
}
}
}