资源简介
设计一个文件管理方案,模拟一个简单的文件管理系统。要求目录结构为树型结构,目录下可以是目录,也可以是文件。通过该实验是学生可以深刻理解文件系统的概念、所涉及的基本操作及实现时的主要数据结构。建议用eclipse打开。
代码片段和文件信息
package file;
import java.io.File;
public class Command {
private String currentPath;
private Mainframe mainframe = null;
private final String cmd[] = { “cd“ “dir“ “md“ “rd“ “edit“ “del“
“exit“ };
private final int cmdInt[] = { 1 2 3 4 5 6 7 };
Command(Mainframe mainframe) {
currentPath = “C:“;
this.mainframe = mainframe;
}
/**
* 函数运行入口;
*
* public static void main(String[] args) {
*
* Command cmd = new Command(); System.out.println(“当前的所有盘符:“);
* cmd.ListDisks(); System.out.print(cmd.getPath()); // 读取用户的命令和数据; while
* (true) { byte[] b = new byte[50]; try { System.in.read(b);
* cmd.operate(new String(b).trim()); } catch (IOException e) {
* e.printStackTrace(); } }
* }
*/
/*
* 显示系统中的所有盘符;
*/
public String[] ListDisks() {
File roots[] = File.listRoots();// 根盘符;
String disks[] = new String[roots.length];
for (int i = 0; i < roots.length; i++) {
disks[i] = roots[i].toString();
}
return disks;
}
public String[] separate(String operation) {
String[] str = operation.split(“ “);// 按分号分割;
//主要解决文件夹或文件中含有空格的情况;
if(str.length>2){
String[] tempStr=new String[2];
tempStr[0]=str[0];
tempStr[1]=str[1];
for(int i=2;i tempStr[1]+=“ “+str[i];
return tempStr;
}
return str;
}
/*
* 根据参数operation执行相应的操作;
*/
public void operate(String operation) {
String[] str = separate(operation);
// System.out.println(str.length);
String mycmd = ““;
// mycmd对应的整数代号;
int mycmdInt = 0;
String path = ““;
if (str.length == 1) {
mycmd = str[0];
if (mycmd.indexOf(“:“) != -1) {// 如果是直接盘符转换;执行些操作;
File newFile = new File(mycmd);
if (newFile.exists()) {
currentPath = mycmd;
// System.out.print(getPath());
mainframe.display(getPath());
return;
}
}
}
if (str.length >= 2) {
mycmd = str[0];
path = str[1];
}
// 选择执行命令;
// ///System.out.println(mycmd+“\\“+path);
for (int i = 0; i < cmd.length; i++) {
if (mycmd.equalsIgnoreCase(cmd[i])) {
mycmdInt = cmdInt[i];
}
}
switch (mycmdInt) {
case 1:
cd(currentPath path);
break;
case 2:
dir(currentPath);
break;
case 3:
md(path);
break;
case 4:
rd(path);
break;
case 5:
edit(path);
break;
case 6:
del(path);
break;
case 7:
exit();
break;
default:
mainframe.display(“无效的命令!“);
}
mainframe.display(getPath());
}
/*
* 获得当前所在目录;
*/
public String getPath() {
return currentPath + “>“;
}
/*
* 获得路径path下的文件;
*/
public String[] listAll(String path) {
try {
File f = new File(path);
String[] fileName;
if (f.isDirectory()) {
fileName = f.list();
mainframe.display(“共有“ + fileName.length + “个文件“);
for (int i = 0; i < fileName.length; i++)
mainframe.display(“ “ + fileName[i]);
return fileName;
} else
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
-rwxrwx--- 506 2006-11-13 14:07 FileManage\.project
-rwxrwx--- 226 2006-11-11 18:37 FileManage\.classpath
-rwxrwx--- 9600 2006-12-30 14:11 FileManage\file\Mainfr
-rwxrwx--- 6757 2006-11-26 10:32 FileManage\file\Command.java
-rwxrwx--- 42 2006-11-16 10:41 FileManage\file\cvs\Root
-rwxrwx--- 7 2006-11-16 10:41 FileManage\file\cvs\Repository
-rwxrwx--- 276 2006-12-20 21:32 FileManage\file\cvs\Entries
-rwxrwx--- 825 2006-11-25 10:28 FileManage\file\FileTreeCellRenderer.java
-rwxrwx--- 85 2004-08-26 19:31 FileManage\file\images\transparent.gif
-rwxrwx--- 904 2004-08-26 19:31 FileManage\file\images\collapsed.gif
-rwxrwx--- 902 2004-08-26 19:31 FileManage\file\images\expanded.gif
-rwxrwx--- 986 2004-08-26 19:31 FileManage\file\images\folderIcon.gif
-rwxrwx--- 917 2004-08-26 19:31 FileManage\file\images\htmlIcon.gif
-rwxrwx--- 10752 2006-11-25 10:16 FileManage\file\images\Thumbs.db
-rwxrwx--- 1039 2006-11-25 10:15 FileManage\file\images\icon_folder.gif
-rwxrwx--- 357 2006-11-25 10:15 FileManage\file\images\icon_folderopen.gif
-rwxrwx--- 226 2006-11-25 10:13 FileManage\file\images\netdisk.gif
-rwxrwx--- 42 2006-11-26 11:08 FileManage\file\images\cvs\Root
-rwxrwx--- 13 2006-11-26 11:08 FileManage\file\images\cvs\Repository
-rwxrwx--- 453 2006-11-26 11:08 FileManage\file\images\cvs\Entries
-rwxrwx--- 1374 2006-11-26 09:39 FileManage\file\FileMouseListener.java
-rwxrwx--- 1948 2006-11-26 10:08 FileManage\file\ExpandListener.java
-rwxrwx--- 1200 2007-05-17 17:02 FileManage\file\Mainfr
-rwxrwx--- 625 2007-05-17 17:02 FileManage\file\Mainfr
-rwxrwx--- 10062 2007-05-17 17:02 FileManage\file\Mainfr
-rwxrwx--- 1116 2007-05-17 17:02 FileManage\file\Mainfr
-rwxrwx--- 1028 2007-05-17 17:02 FileManage\file\Mainfr
-rwxrwx--- 1135 2007-05-17 17:02 FileManage\file\FileTreeCellRenderer.class
-rwxrwx--- 2088 2007-05-17 17:02 FileManage\file\FileMouseListener.class
-rwxrwx--- 2594 2007-05-17 17:02 FileManage\file\ExpandListener.class
............此处省略48个文件信息
评论
共有 条评论