-
大小: 40KB文件類型: .rar金幣: 2下載: 1 次發(fā)布日期: 2021-06-08
- 語言: Java
- 標(biāo)簽:
資源簡(jiǎn)介
設(shè)計(jì)一個(gè)文件管理方案,模擬一個(gè)簡(jiǎn)單的文件管理系統(tǒng)。要求目錄結(jié)構(gòu)為樹型結(jié)構(gòu),目錄下可以是目錄,也可以是文件。通過該實(shí)驗(yàn)是學(xué)生可以深刻理解文件系統(tǒng)的概念、所涉及的基本操作及實(shí)現(xiàn)時(shí)的主要數(shù)據(jù)結(jié)構(gòu)。建議用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;
}
/**
?*?函數(shù)運(yùn)行入口;
?*?
?*?public?static?void?main(String[]?args)?{
?*?
?*?Command?cmd?=?new?Command();?System.out.println(“當(dāng)前的所有盤符:“);
?*?cmd.ListDisks();?System.out.print(cmd.getPath());?//?讀取用戶的命令和數(shù)據(jù);?while
?*?(true)?{?byte[]?b?=?new?byte[50];?try?{?System.in.read(b);
?*?cmd.operate(new?String(b).trim());?}?catch?(IOException?e)?{
?*?e.printStackTrace();?}?}
?*??}
?*/
/*
?*?顯示系統(tǒng)中的所有盤符;
?*/
public?String[]?ListDisks()?{
File?roots[]?=?File.listRoots();//?根盤符;
String?disks[]?=?new?String[roots.length];
for?(int?i?=?0;?i? disks[i]?=?roots[i].toString();
}
return?disks;
}
public?String[]?separate(String?operation)?{
String[]?str?=?operation.split(“?“);//?按分號(hào)分割;
//主要解決文件夾或文件中含有空格的情況;
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;
}
/*
?*?根據(jù)參數(shù)operation執(zhí)行相應(yīng)的操作;
?*/
public?void?operate(String?operation)?{
String[]?str?=?separate(operation);
//?System.out.println(str.length);
String?mycmd?=?““;
//?mycmd對(duì)應(yīng)的整數(shù)代號(hào);
int?mycmdInt?=?0;
String?path?=?““;
if?(str.length?==?1)?{
mycmd?=?str[0];
if?(mycmd.indexOf(“:“)?!=?-1)?{//?如果是直接盤符轉(zhuǎn)換;執(zhí)行些操作;
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];
}
//?選擇執(zhí)行命令;
//?///System.out.println(mycmd+“\\“+path);
for?(int?i?=?0;?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());
}
/*
?*?獲得當(dāng)前所在目錄;
?*/
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?+?“個(gè)文件“);
for?(int?i?=?0;?i? mainframe.display(“????“?+?fileName[i]);
return?fileName;
}?else
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?-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個(gè)文件信息
評(píng)論
共有 條評(píng)論