import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.io.file.FileSystemRegistry;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class FileProperties extends MIDlet implements CommandListener {
Display display;
List list = new List("浏览文件和文件夹", List.IMPLICIT);
/**
* 表示根目录的字符串
*/
private final static String ROOT = "/";
/**
* 表示父目录的字符串
*/
private final static String PARENT_FOLDER = "..";
//文件和目录的图标
Image imgFile = null; //文件图标
Image imgDir = null; //目录图标
Image imgRoot = null; //根目录图标
//保存当前的路径,缺省为根路径
String curURI = ROOT;
public FileProperties() {
super();
//装载文件图标
try {
imgFile = Image.createImage("/txtfile.JPG");
} catch (IOException e) {
System.out.println("不能装载文件图标");
}
//装载目录图标
try {
imgDir = Image.createImage("/dirclose.JPG");
} catch (IOException e) {
System.out.println("不能装载目录图标");
}
//装载根目录图标
try {
imgRoot = Image.createImage("/drive.JPG");
} catch (IOException e) {
System.out.println("不能装载根目录图标");
}
}
protected void startApp() throws MIDletStateChangeException {
String name = "microedition.io.file.FileConnection.version";
String value = System.getProperty( name );
if (value != null) {
System.out.println("文件系统API版本:" + value);
} else {
System.out.println("当前手机不支持文件系统API");
return;
}
list.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(list);
displayDir();
}
protected void pauseApp() {
// TODO 自动生成方法存根
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
}
/**
* 显示变量curDir中的全部的文件和子文件夹
*
*/
private void displayDir() {
list.deleteAll();
if (curURI.equals(ROOT)) {
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements()) {
String rootName = (String)e.nextElement();
list.append(rootName, imgRoot);
}
} else {
//显示目录中的全部文件和文件夹
list.append(PARENT_FOLDER, imgDir); //添加父目录
try {
FileConnection fc = (FileConnection)Connector.open(
"file://"+curURI);
Enumeration e = fc.list();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
if (name.indexOf("/")>-1) {
//添加目录
list.append(name, imgDir);
} else {
//添加文件
list.append(name, imgFile);
}
}
fc.close();
}
catch (IOException ioe) {
}
}
}
/**
* 处理命令按钮事件
*/
public void commandAction(Command c, Displayable d) {
if (c == List.SELECT_COMMAND) {
int index = list.getSelectedIndex();
String name = list.getString(index);
if (name == PARENT_FOLDER) {
//返回上级目录
int i = curURI.lastIndexOf('/',
curURI.length()-2);
if (i != -1) {
//获得父目录名字
curURI = curURI.substring(0, i+1);
} else {
curURI = ROOT; //根目录
}
} else {
//判断是否为子目录
String newname = curURI + name;
System.out.println("选择目标为:"+ newname);
if (name.indexOf("/")>-1) {
curURI = newname;
} else {
//如果为文件则显示属性
getProperties(newname);
}
}
//在线程中显示当前目录
new Thread() {
public void run() {
displayDir();
}
}.start();
}
}
private void getProperties(String name) {
try {
FileConnection fc = (FileConnection)Connector.open(
"file://"+name);
if (fc.exists()) {
System.out.println("可以被读:"+ fc.canRead());
System.out.println("可以被写:"+ fc.canWrite());
System.out.println("隐藏:"+ fc.isHidden());
System.out.println("位置:"+ curURI);
System.out.println("类型:"+
(fc.isDirectory() ? "文件夹": "文件"));
System.out.println("文件大小:"+ fc.fileSize());
System.out.println("修改时间:"+
formatTime(fc.lastModified()));
System.out.println("文件系统的总空间大小:" +
fc.totalSize());
System.out.println("文件系统中被使用的空间大小:" +
fc.usedSize());
}
fc.close();
} catch (Exception e) {
}
}
/**
* 格式化文件的时间,以中文习惯输出
*/
private String formatTime(long time) {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(time));
StringBuffer sb = new StringBuffer();
sb.append(cal.get(Calendar.YEAR));
sb.append("年");
sb.append(cal.get(Calendar.MONTH));
sb.append("月");
sb.append(cal.get(Calendar.DAY_OF_MONTH)+1);
sb.append("日 ");
sb.append(cal.get(Calendar.HOUR_OF_DAY));
sb.append(':');
sb.append(cal.get(Calendar.MINUTE));
sb.append(':');
sb.append(cal.get(Calendar.SECOND));
return sb.toString();
}
}