?? fileexplorer.java
字號:
display.setCurrent(frmPropertys);
} catch (Exception e) {
Alert alert = new Alert("錯誤!",
"不能訪問目錄 " + currDirName +
" 中的文件 " + fileName +
"\n發生異常:" + e.toString(),
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
/**
* 格式化文件的時間,以中文習慣輸出
*/
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();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command c, Displayable d) {
if (c == cmdView) {
List curr = (List)d;
final String currFile = curr.getString(curr.getSelectedIndex());
//在線程中執行操作
new Thread(new Runnable() {
public void run() {
if (currFile.endsWith(FOLDER_END_STR) ||
currFile.equals(FATHER_FOLDER)) {
//目錄跳轉
traverseDirectory(currFile);
} else {
//顯示文件內容
showFile(currFile);
}
}
}).start();
} else if (c == cmdProperty) {
//顯示文件或者目錄的屬性
List curr = (List)d;
String currFile = curr.getString(curr.getSelectedIndex());
showPropertyerties(currFile);
} else if (c == cmdCreate) {
//選擇新建命令按鈕后,首先顯示創建文件或者文件夾的窗口
showCreateFileForm();
} else if (c == cmdCreateOK) {
//在創建文件或者文件夾的窗口中輸入完畢后,單擊確定按鈕后,
//執行這里的代碼
String newName = nameInput.getString();
if (newName == null || newName.equals("")) {
Alert alert = new Alert("錯誤!",
"文件名為空,請輸入文件名",
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
} else {
executeCreateeFile(newName,
typeInput.getSelectedIndex() != 0);
Display.getDisplay(feInstance).
getCurrent().removeCommand(cmdCreateOK);
Display.getDisplay(feInstance).
getCurrent().removeCommand(cmdBack);
}
} else if (c == cmdBack) {
showCurrDir();
} else if (c == cmdExit) {
feInstance.destroyApp(false);
} else if(c == cmdDelete) {
final List curr = (List)d;
new Thread(new Runnable(){
public void run(){
delete(curr.getString(curr.getSelectedIndex()));
}
}).start();
} else if (c == cmdCopy) {
final List curr = (List)d;
new Thread(new Runnable(){
public void run(){
copy(curr.getString(curr.getSelectedIndex()));
}
}).start();
} else if (c == cmdCut) {
final List curr = (List)d;
cutFlag = true;
new Thread(new Runnable(){
public void run(){
copy(curr.getString(curr.getSelectedIndex()));
}
}).start();
} else if (c == cmdPaste) {
if ((this.clipboardFileName != null) &&
(this.clipboardDir != null)) {
new Thread(new Runnable(){
public void run(){
paste();
}
}).start();
}
}
}
/**
* 在另外的一個線程中創建文件或者文件夾
*/
private void executeCreateeFile(final String name, final boolean val) {
new Thread(new Runnable(){
public void run(){
createFile(name, val);
}
}).start();
}
/**
* 創建一個文件或者文件夾
* <p>
* @param newName
*/
void createFile(String newName, boolean isDirectory) {
try {
FileConnection fc = (FileConnection) Connector.open("file:///" +
currDirName + newName);
if (isDirectory) {
fc.mkdir();
} else {
fc.create();
}
fc.close();
showCurrDir();
} catch (Exception e) {
String s = "不能創建文件 '" + newName + "'";
if (e.getMessage() != null && e.getMessage().length() > 0) {
s += "\n" + e;
}
Alert alert = new Alert("錯誤!", s, null, AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
//恢復刪除的命令按鈕
display.getCurrent().addCommand(cmdCreateOK);
display.getCurrent().addCommand(cmdBack);
}
}
/**
* 刪除文件或者目錄
* <p>
* @param fileName 要刪除的文件或者目錄
*/
void delete(String fileName) {
try {
if (fileName.equals(FATHER_FOLDER)) {
return;
}
FileConnection fc = (FileConnection)Connector.open(
"file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("文件不存在");
}
if (fc.isDirectory()) {
throw new IOException("目前不支持文件夾的刪除");
}
//當刪除文件或者目錄時顯示對話框讓用戶確認操作
ConfirmDialog cd = new ConfirmDialog(display, "刪除文件",
"確認要刪除文件嗎?");
if (cd.showDialog()== ConfirmDialog.MR_OK) {
fc.delete();
}
fc.close();
showCurrDir();
} 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 copy(String fileName) {
try {
if (fileName.equals(FATHER_FOLDER)) {
return;
}
FileConnection fc = (FileConnection)Connector.open(
"file://localhost/" + currDirName + fileName);
if (!fc.exists()) {
throw new IOException("文件不存在");
}
if (fc.isDirectory()) {
throw new IOException("不支持文件夾的復制");
}
this.clipboardFileName = fileName;
this.clipboardDir = currDirName;
fc.close();
} catch (Exception e) {
Alert alert = new Alert("錯誤!",
"不能訪問目錄 " + currDirName +
" 中的文件 " + fileName +
"\n發生異常:" + e.toString(),
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
/**
* 從粘貼板復制文件到當前目錄
*/
void paste() {
String fileName = clipboardFileName;
try {
if (fileName.equals(FATHER_FOLDER)) {
return;
}
FileConnection fcSrc = (FileConnection)Connector.open(
"file://localhost/" + clipboardDir + fileName);
FileConnection fcDst = (FileConnection)Connector.open(
"file://localhost/" + currDirName + fileName);
if (!fcSrc.exists()) {
throw new IOException("文件不存在");
}
if (fcDst.exists()) {
//確認覆蓋操作
ConfirmDialog cd = new ConfirmDialog(display, "粘貼文件",
"文件已經存在,確認覆蓋嗎?");
if (cd.showDialog()== ConfirmDialog.MR_OK) {
fcDst.delete();
}
}
if (fcSrc.isDirectory()) {
throw new IOException("不支持文件夾的復制和粘貼");
}
fcDst.create();
InputStream is = fcSrc.openInputStream();
OutputStream os = fcDst.openOutputStream();
byte[] buffer = new byte[MAX_FILE_LENGTH];
is.read(buffer);
os.write(buffer);
os.flush();
if (cutFlag) {
//剪切操作,要刪除原來的文件
fcSrc.delete();
}
fcDst.close();
fcSrc.close();
//復制完成后清空剪切板
this.clipboardFileName = null;
this.clipboardDir = null;
cutFlag = false;
showCurrDir();
} catch (Exception e) {
Alert alert = new Alert("錯誤!",
"不能訪問目錄 " + clipboardDir +
" 中的文件 " + fileName +
"\n發生異常:" + e.toString(),
null,
AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -