📄 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 + -