?? fileexplorer.java
字號:
/*
* FileExplorer.java
*
* cmdCreateed on 2005年3月18日, 下午11:21
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.io.file.*;
/**
* 本程序實現了一個簡單的文件瀏覽器,主要具有以下功能:
* <p>1、文件目錄瀏覽
* <p>2、新建目錄或者文件
* <p>3、刪除目錄或者文件
* <p>4、復制文件
* <p>5、剪切文件
* <p>6、粘貼文件
* <p>7、訪問目錄或者文件的屬性
* <p>8、訪問文件的內容
* @author Liu Bin
*/
public class FileExplorer implements CommandListener{
//保存MIDlet實例
static FileExplorerDemo feInstance = null;
//保存MIDet的Display
private Display display = null;
private String currDirName;
private Command cmdView = new Command("查看", Command.ITEM, 1);
private Command cmdCreate = new Command("新建", Command.ITEM, 2);
private Command cmdDelete = new Command("刪除", Command.ITEM, 3);
private Command cmdCopy = new Command("復制", Command.ITEM, 4);
private Command cmdPaste = new Command("粘貼", Command.ITEM, 5);
private Command cmdCut = new Command("剪切", Command.ITEM, 5);
private Command cmdProperty = new Command("屬性", Command.ITEM, 2);
private Command cmdCreateOK = new Command("確定", Command.OK, 1);
private Command cmdBack = new Command("返回", Command.BACK, 2);
private Command cmdExit = new Command("退出", Command.EXIT, 3);
//創建文件或者目錄時,用于輸入文件或者目錄的名字
private TextField nameInput;
//在創建文件或者目錄時,用于選擇是創建文件還是創建目錄
private ChoiceGroup typeInput;
private final static String[] attrList = { "讀", "寫", "隱藏" };
private final static String[] typeList = { "文件", "目錄" };
/** 文件和目錄的圖標 */
private Image dirIcon, fileIcon;
private Image[] iconList;
/** 表示父目錄的字符串 */
private final static String FATHER_FOLDER = "..";
/**
* 表示根目錄的字符串
*/
private final static String ROOT = "/";
/** 規范中定義的,文件夾結尾的字符串 */
private final static String FOLDER_END_STR = "/";
/** 規范中定義的,文件夾結尾的字符 */
private final static char FOLDER_END_CHAR = '/';
/** 可以訪問的文件最大長度 */
public final static int MAX_FILE_LENGTH = 4096;
/** 下面的三個變量用于復制和移動文件 */
private String clipboardFileName=null;
private String clipboardDir=null;
private boolean cutFlag = false;
/** 創建一個文件瀏覽器實例 */
public FileExplorer(FileExplorerDemo instance) {
feInstance = instance;
display = Display.getDisplay(feInstance);
//缺省的路徑為根目錄
currDirName = ROOT;
//裝載文件和目錄圖標
try {
dirIcon = Image.createImage("/folder.png");
} catch (IOException e) {
dirIcon = null;
}
try {
fileIcon = Image.createImage("/file.png");
} catch (IOException e) {
fileIcon = null;
}
//設置ChoiceGroup的圖標
iconList = new Image[] { fileIcon, dirIcon };
}
/**
* 必須執行該方法后,文件瀏覽器才開始運行
*/
public void open() {
try {
showCurrDir(); //顯示目錄和文件
} catch (SecurityException e) {
//如果有安全限制,則執行下面的代碼
Alert alert = new Alert("錯誤",
"您沒有權限訪問這個受限制的API",
null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
Form form = new Form("不能訪問FileConnection");
form.append(new StringItem(null,
"以當前的權限,MIDlet不能夠訪問受限制的API,請給MIDlet簽名"
+ "或者運行在其他的安全域上"));
form.addCommand(cmdExit);
form.setCommandListener(this);
Display.getDisplay(feInstance).setCurrent(alert, form);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 顯示當前目錄中的文件和目錄列表
*/
void showCurrDir() {
Enumeration e;
FileConnection currDir = null;
List browser;
try {
if (currDirName.equals(ROOT)) {
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(FATHER_FOLDER, dirIcon);
}
while (e.hasMoreElements()) {
String fileName = (String)e.nextElement();
if (fileName.charAt(fileName.length()-1) == FOLDER_END_CHAR) {
//增加目錄
browser.append(fileName, dirIcon);
} else {
//增加文件
browser.append(fileName, fileIcon);
}
}
browser.setSelectCommand(cmdView);
//根目錄不能添加下面這些命令按鈕
if (!ROOT.equals(currDirName)) {
browser.addCommand(cmdCreate);
browser.addCommand(cmdProperty);
browser.addCommand(cmdDelete);
browser.addCommand(cmdCopy);
browser.addCommand(cmdCut);
browser.addCommand(cmdPaste);
}
browser.addCommand(cmdExit);
browser.setCommandListener(this);
if (currDir != null) {
currDir.close();
}
display.setCurrent(browser);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 進入其他目錄
* <p>
* @param fileName 要進入的目錄
*/
void traverseDirectory(String fileName) {
if (currDirName.equals(ROOT)) {
if (fileName.equals(FATHER_FOLDER)) {
//確保根目錄為頂級目錄
return;
}
currDirName = fileName;
} else if (fileName.equals(FATHER_FOLDER)) {
//返回上級目錄
int i = currDirName.lastIndexOf(FOLDER_END_CHAR,
currDirName.length()-2);
if (i != -1) {
//獲得父ss目錄名字
currDirName = currDirName.substring(0, i+1);
} else {
currDirName = ROOT; //根目錄
}
} else {
currDirName = currDirName + fileName;
}
showCurrDir();
}
/**
* 在創建一個文件或者文件夾時,顯示一個窗口用于輸入名字和選擇類型
*/
void showCreateFileForm() {
Form frmCreateor = new Form("創建文件/文件夾");
nameInput = new TextField("請輸入文件名", null, 256, TextField.ANY);
typeInput = new ChoiceGroup("請選擇文件類型", Choice.EXCLUSIVE,
typeList, iconList);
frmCreateor.append(nameInput);
frmCreateor.append(typeInput);
frmCreateor.addCommand(cmdCreateOK);
frmCreateor.addCommand(cmdBack);
frmCreateor.addCommand(cmdExit);
frmCreateor.setCommandListener(this);
display.setCurrent(frmCreateor);
}
/**
* 顯示選擇的文件內容
* <p>
* @param fileName 要顯示的文件名
*/
void showFile(String fileName) {
try {
FileConnection fc = (FileConnection)
Connector.open("file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("文件不存在");
}
InputStream fis = fc.openInputStream();
byte[] b = new byte[MAX_FILE_LENGTH];
int length = fis.read(b, 0, MAX_FILE_LENGTH);
fis.close();
fc.close();
TextBox tbViewer = new TextBox("查看文件: " + fileName,
null, MAX_FILE_LENGTH,
TextField.ANY | TextField.UNEDITABLE);
tbViewer.addCommand(cmdBack);
tbViewer.addCommand(cmdExit);
tbViewer.setCommandListener(this);
if (length > 0) {
tbViewer.setString(new String(b, 0, length));
}
display.setCurrent(tbViewer);
} catch (Exception e) {
Alert alert = new Alert("錯誤!",
"不能訪問目錄 " + currDirName +
" 中的文件 " + fileName +
"\n發生異常:" + e.toString(),
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
/**
* 顯示目錄或者文件的屬性
* <p>
* @param fileName 要查看屬性的文件或者目錄
*/
void showPropertyerties(String fileName) {
try {
if (fileName.equals(FATHER_FOLDER)) {
return;
}
FileConnection fc = (FileConnection)Connector.open(
"file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("文件不存在");
}
Form frmPropertys = new Form("cmdPropertyerties: " + fileName);
ChoiceGroup attrs = new ChoiceGroup("屬性:", Choice.MULTIPLE,
attrList, null);
attrs.setSelectedFlags(new boolean[] {fc.canRead(),
fc.canWrite(),
fc.isHidden()}
);
frmPropertys.append(new StringItem("位置:", currDirName));
frmPropertys.append(new StringItem("類型:",
fc.isDirectory() ? "文件夾": "文件"));
frmPropertys.append(new StringItem("文件大小:",
String.valueOf(fc.fileSize())));
frmPropertys.append(new StringItem("修改時間:",
formatTime(fc.lastModified())));
frmPropertys.append(attrs);
frmPropertys.addCommand(cmdBack);
frmPropertys.addCommand(cmdExit);
frmPropertys.setCommandListener(this);
fc.close();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -