沒有持久化存儲到本地系統(tǒng)盤,演示結果:使用Java語言實現(xiàn),附帶C++版本
目前創(chuàng)新互聯(lián)建站已為1000+的企業(yè)提供了網(wǎng)站建設、域名、雅安服務器托管、網(wǎng)站托管、企業(yè)網(wǎng)站設計、蘭州網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。邏輯解釋主要的兩個類:文件夾和文件
class FOLDER{//目錄結構
String name=new String();//目錄名
FOLDER nextFolder;//同級下一目錄
FOLDER frontFolder;//同級上一目錄
FOLDER parentFolder;//父目錄
FOLDER firstChildFolder;//子目錄
FOLDER lastChildFolder;//最后一個子文件夾
FILE firstChildFile;//子文件
FILE lastChildFile;
public FOLDER(String name, FOLDER nextFolder, FOLDER frontFolder, FOLDER parentFolder, FOLDER firstChildFolder, FOLDER lastChildFolder, FILE firstChildFile, FILE lastChildFile)
{this.name = name;
this.nextFolder = nextFolder;
this.frontFolder = frontFolder;
this.parentFolder = parentFolder;
this.firstChildFolder = firstChildFolder;
this.lastChildFolder = lastChildFolder;
this.firstChildFile = firstChildFile;
this.lastChildFile = lastChildFile;
}
public FOLDER() {}
}
class FILE{String name=new String();//文件名
String content=new String();//文件內(nèi)容
FILE frontFile;//同級目錄上一文件
FILE nextFile;//同級目錄下一文件
FOLDER parentFolder;//父目錄
public FILE(String name, String content, FILE frontFile, FILE nextFile, FOLDER parentFolder) {this.name = name;
this.content = content;
this.frontFile = frontFile;
this.nextFile = nextFile;
this.parentFolder = parentFolder;
}
public FILE() {}
}
解釋:有一個總的root文件夾,每一個文件夾類,采用類似鏈表的連接方式,記錄當前目錄下的第一個文件夾和最后一個文件夾,可以遍歷查找,而文件也是同樣的方式。
代碼實現(xiàn)實現(xiàn)的Java代碼:
import java.util.Objects;
import java.util.Scanner;
public class FileSystem {
FOLDER root,nowpath;
public static void main(String[] args) {
FileSystem init = new FileSystem();
init.createRoot();
init.menu();
}
// 切換目錄
void open_dir(String name) {
// 返回上一層目錄
if(Objects.equals(name, "../")){
if(nowpath!=root)
nowpath = nowpath.parentFolder;
return;
}
FOLDER a = nowpath.firstChildFolder;
while(a!=null && !Objects.equals(a.name, name))
a = a.nextFolder;
if(a==null || !Objects.equals(a.name, name)){
System.out.println("沒有找到"+name);
return;
}
nowpath = a;
}
// 展示目錄
void showInfo() {
FOLDER x = nowpath.firstChildFolder;
while(x!=null){
System.out.println(x.name);
x = x.nextFolder;
}
FILE y = nowpath.firstChildFile;
while (y!=null){
System.out.println(y.name);
y = y.nextFile;
}
}
// 修改文件
void modify(String name) {
FILE a = nowpath.firstChildFile;
FILE b = nowpath.lastChildFile;
while (a!=null && !Objects.equals(a.name, name))
a = a.nextFile;
if(a==null || !Objects.equals(a.name, name)){
System.out.println("沒有找到"+name);
return;
}
System.out.print("選擇續(xù)寫: 1\n覆蓋寫入: 2\n");
Scanner in = new Scanner(System.in);
int op = in.nextInt();
if(op==2) a.content = "";
System.out.println("輸入內(nèi)容");
in.nextLine(); //吞掉換行符
String s = in.nextLine();
System.out.println(s+"-------檢測輸入");
if(a.content!=null) a.content = a.content+s;
else a.content = s;
}
// 查看文件內(nèi)容
void show_context(String name) {
FILE a = nowpath.firstChildFile;
while(a!=null && !Objects.equals(a.name, name))
a = a.nextFile;
if(a==null || !Objects.equals(a.name, name)){
System.out.println("沒有找到"+name+"文件");
return;
}
else System.out.println(a.content);
}
// 刪除文件
void rm(String name) {
boolean flag = false;
FILE a = nowpath.firstChildFile;
FILE b = nowpath.lastChildFile;
if(a==null){
if (flag) System.out.println("成功刪除");
else System.out.println("不存在"+name+"文件");
return;
}
// 第一個文件就是要找的文件
if(a!=null && Objects.equals(a.name, name)){
flag = true;
boolean fl = a == b ? true : false;
FILE nxt = a.nextFile;//是否還有其他文件
nowpath.firstChildFile = nxt;
if (nxt != null)
nxt.frontFile = null;
if (fl)
nowpath.lastChildFile = null;
}
else if(b!=null && Objects.equals(b.name, name)){
flag = true;
FILE pre = b.frontFile;
nowpath.lastChildFile = pre;
if(pre!=null)
pre.nextFile = null;
}
else { // 不是第一個和最后一個文件,從頭遍歷查找此文件
if(a.nextFile!=null) a = a.nextFile;
while (a!=null && a!=b && !Objects.equals(a.name, name))
a = a.nextFile;
if (a != null && Objects.equals(a.name, name))
{
flag = true;
FILE pre = a.frontFile;
FILE nxt = a.nextFile;
if (pre != null)
pre.nextFile = nxt;
if (nxt != null)
nxt.frontFile = pre;
}
}
if (flag) System.out.println("成功刪除");
else System.out.println("不存在"+name+"文件");
}
// 創(chuàng)建文件
void touch_file(String name) {
FILE x = nowpath.firstChildFile;
FILE y = nowpath.lastChildFile;
boolean flag = true;
while (x!=y){
if(Objects.equals(x.name, name))
flag = false;
x = x.nextFile;
}
if(y!=null && Objects.equals(y.name, name))
flag = false;
if(!flag){
System.out.println("當前文件名已存在");
return;
}
FILE newFile = new FILE(name,null,null,null,nowpath);
if (nowpath.firstChildFile == null){
nowpath.firstChildFile = newFile;
nowpath.lastChildFile = newFile;
}
else
{
FILE b = nowpath.lastChildFile;
b.nextFile = newFile;
newFile.frontFile = b;
nowpath.lastChildFile = newFile;
}
}
void del(FOLDER a){
if (a == null) return;
FILE x = a.firstChildFile;
FILE y = a.lastChildFile;
while (x != y)
{
FILE t = x.nextFile;
x = t;
}
FOLDER xF = a.firstChildFolder;
FOLDER yF = a.lastChildFolder;
while (xF != yF)
{
FOLDER t = xF.nextFolder;
del(xF);
xF = t;
}
if (yF != null)
{
del(yF);
}
}
// 刪除文件夾
void remove_directory(String name) {
boolean flag = false;
FOLDER a = nowpath.firstChildFolder;
FOLDER b = nowpath.lastChildFolder;
if(a==null){
if(flag) System.out.println("成功刪除");
else System.out.println("沒有"+name+"文件夾");
return;
}
if(a!=null && Objects.equals(a.name, name)){
flag = true;
boolean f1 = b == a ? true : false;
FOLDER nxt = a.nextFolder;
del(a);
nowpath.firstChildFolder = nxt;
if (nxt != null)
nxt.frontFolder = null;
if (flag) //同時又是最后一個
nowpath.lastChildFolder = null;
}
else if(b!=null && Objects.equals(b.name, name)){
flag = true;
FOLDER pre = b.frontFolder;
del(pre);
nowpath.lastChildFolder = pre;
if (pre != null)
pre.nextFolder = null;
}
else {
a = a.nextFolder;
// 遞歸刪除
while (a != null && a != b && !Objects.equals(a.name, name))
a = a.nextFolder;
// 刪除最后的文件夾
if (a != null && Objects.equals(a.name, name))
{
flag = true;
FOLDER pre = a.frontFolder;
FOLDER nxt = a.nextFolder;
del(a);
if (pre != null)
pre.nextFolder = nxt;
if (nxt != null)
nxt.frontFolder = pre;
}
}
if(flag) System.out.println("成功刪除");
else System.out.println("沒有"+name+"文件夾");
}
// 添加文件夾
void make_directory(String name)
{
FOLDER x = nowpath.firstChildFolder;
FOLDER y = nowpath.lastChildFolder;
boolean flag = true;
// 是否有同名的子目錄,有同名不讓創(chuàng)建
while (x != y)
{
if (Objects.equals(x.name, name)) flag = false;
x = x.nextFolder;
}
if (y != null && Objects.equals(y.name, name))
flag = false;
if(!flag)
{
System.out.println(name + "文件夾已存在");
return;
}
// 無同名文件夾,開始創(chuàng)建文件夾
FOLDER newFolder = new FOLDER(name,null,null,
nowpath,null,null,null,null);
if(nowpath.firstChildFolder==null){
nowpath.firstChildFolder = newFolder;
nowpath.lastChildFolder = newFolder;
}
else
{
// 更新當前文件夾下的第一個子文件夾和最后一個子文件夾 (鏈表的形式)
FOLDER tmp = nowpath.lastChildFolder;
tmp.nextFolder = newFolder;
newFolder.frontFolder = tmp;
nowpath.lastChildFolder = newFolder;
}
}
String getPath()
{
String path = nowpath.name+">";
FOLDER now = nowpath;
while(now.parentFolder!=null)
{
now = now.parentFolder;
path = now.name +"/"+path;
}
return path;
}
void Operation(String op,String name)
{
if(Objects.equals(op, "exit")){
System.out.println("操作結束!!!");
System.exit(0);
}
if(Objects.equals(op, "mkdir")) make_directory(name);
else if(Objects.equals(op, "rmdir")) remove_directory(name);
else if(Objects.equals(op, "touch")) touch_file(name);
else if(Objects.equals(op, "rm")) rm(name);
else if(Objects.equals(op, "cat")) show_context(name);
else if(Objects.equals(op, "vim")) modify(name);
else if(Objects.equals(op, "ls")) showInfo();
else if(Objects.equals(op, "cd")) open_dir(name);
else System.out.println(op + "命令沒找到");
}
void menu()
{
Scanner in = new Scanner(System.in);
System.out.println(" 創(chuàng)建目錄 -- mkdir\n" +
" 刪除目錄 -- rmdir\n" +
" 切換目錄 -- cd\n" +
" 創(chuàng)建文件 -- touch\n" +
" 刪除文件 -- rm\n" +
" 查看文件內(nèi)容 -- cat\n" +
" 寫入文件 -- vim\n" +
" 查看目錄 -- ls\n" +
" 退出系統(tǒng) -- exit");
while(true)
{
System.out.print(getPath());
String ss = in.nextLine();
String[] s = ss.split(" ");
if(s.length>2) System.out.println("輸入有誤");
String op = new String();
String name = new String();
op = s[0];
if(s.length==2 )name = s[1];
Operation(op,name);
}
}
void createRoot() {//初始化根目錄信息
root=new FOLDER();
root.frontFolder=null;
root.nextFolder=null;
root.parentFolder=null;
root.firstChildFile=null;
root.firstChildFolder=null;;
root.lastChildFile = null;
root.name="root";
nowpath = root;
}
}
class FOLDER{//目錄結構
String name=new String();//目錄名
FOLDER nextFolder;//同級下一目錄
FOLDER frontFolder;//同級上一目錄
FOLDER parentFolder;//父目錄
FOLDER firstChildFolder;//子目錄
FOLDER lastChildFolder;//最后一個子文件夾
FILE firstChildFile;//子文件
FILE lastChildFile;
public FOLDER(String name, FOLDER nextFolder, FOLDER frontFolder, FOLDER parentFolder, FOLDER firstChildFolder, FOLDER lastChildFolder, FILE firstChildFile, FILE lastChildFile)
{
this.name = name;
this.nextFolder = nextFolder;
this.frontFolder = frontFolder;
this.parentFolder = parentFolder;
this.firstChildFolder = firstChildFolder;
this.lastChildFolder = lastChildFolder;
this.firstChildFile = firstChildFile;
this.lastChildFile = lastChildFile;
}
public FOLDER() {}
}
class FILE{
String name=new String();//文件名
String content=new String();//文件內(nèi)容
FILE frontFile;//同級目錄上一文件
FILE nextFile;//同級目錄下一文件
FOLDER parentFolder;//父目錄
public FILE(String name, String content, FILE frontFile, FILE nextFile, FOLDER parentFolder) {
this.name = name;
this.content = content;
this.frontFile = frontFile;
this.nextFile = nextFile;
this.parentFolder = parentFolder;
}
public FILE() {}
}
C++版本:
#includeusing namespace std;
struct Folder;
struct File
{
string name;
string context;
int canRead;
int canWrite;
File* frontFile;
File* nextFile;
Folder* parent;
};
struct Folder
{
string name;
int canRead;
int canWrite;
Folder* nextFolder;
Folder* frontFolder;
Folder* parent;
Folder* firstChildFolder;
Folder* lastChildFolder;
File* firstChildFile;
File* lastChildFile;
};
Folder* nowpath,* root;
void createroot()
{
root = new Folder{
name : "root",
canRead : 1,
canWrite : 0,
nextFolder : nullptr,
frontFolder : nullptr,
parent : nullptr,
firstChildFolder : nullptr,
firstChildFile : nullptr,
lastChildFile : nullptr
};
nowpath = root;
}
string getpath() //獲取當前文件夾路徑
{
string path = nowpath->name + ">";
Folder *now = nowpath;
while (now ->parent != nullptr)
{
now = now->parent;
path = now->name + "/" + path;
}
return path;
}
void make_directory(string name) //創(chuàng)建目錄
{
auto x = nowpath->firstChildFolder;
auto y = nowpath->lastChildFolder;
bool flag = true;
while (x != y)
{
if (x->name == name)
flag = false;
x = x->nextFolder;
}
if (y != nullptr && y->name == name)
flag = false;
if (!flag)
{
cout<< "cannot create directory '" + name + "': Directory exists\n";
return;
}
Folder *newFolder = new Folder{
name : name,
canRead : 1,
canWrite : 0,
nextFolder : nullptr,
frontFolder : nullptr,
parent : nowpath,
firstChildFolder : nullptr,
lastChildFolder : nullptr,
firstChildFile : nullptr,
lastChildFile : nullptr
};
if (nowpath->firstChildFolder == nullptr)
nowpath->firstChildFolder = nowpath->lastChildFolder = newFolder;
else
{
auto b = nowpath->lastChildFolder;
b->nextFolder = newFolder;
newFolder->frontFolder = b;
nowpath->lastChildFolder = newFolder;
}
}
void del(Folder *a) //遞歸刪除a的所有子文件
{
if (a == nullptr) return;
auto x = a->firstChildFile;
auto y = a->lastChildFile;
while (x != y)
{
auto t = x->nextFile;
free(x);
x = t;
}
if (y != nullptr)
free(y);
auto xF = a->firstChildFolder;
auto yF = a->lastChildFolder;
while (xF != yF)
{
auto t = xF->nextFolder;
del(xF);
free(xF);
xF = t;
}
if (yF != nullptr)
{
del(yF);
free(yF);
}
}
void remove_directory(string name)
{
bool flag = false;
auto a = nowpath->firstChildFolder;
auto b = nowpath->lastChildFolder;
if (a == nullptr) goto cc;
if (a != nullptr && a->name == name)
{
flag = true;
bool fl = b == a ? true : false;
auto nxt = a->nextFolder;
del(a);
free(a);
nowpath->firstChildFolder = nxt;
if (nxt != nullptr)
nxt->frontFolder = nullptr;
if (flag) //同時又是最后一個
nowpath->lastChildFolder = nullptr;
}
else if (b != nullptr && b->name == name)
{
flag = true;
auto pre = b->frontFolder;
del(b);
free(b);
nowpath->lastChildFolder = pre;
if (pre != nullptr)
pre->nextFolder = nullptr;
}
else
{
a = a->nextFolder;
while (a != nullptr && a != b && a->name != name)
a = a->nextFolder;
if (a != nullptr && a->name == name)
{
flag = true;
auto pre = a->frontFolder;
auto nxt = a->nextFolder;
del(a);
free(a);
if (pre != nullptr)
pre->nextFolder = nxt;
if (nxt != nullptr)
nxt->frontFolder = pre;
}
}
cc: if (flag)
cout<< "OK\n";
else cout<< "failed to remove '" + name + "' No such directory\n";
}
void touch_file(string name)
{
auto x = nowpath->firstChildFile;
auto y = nowpath->lastChildFile;
bool flag = true;
while (x != y)
{
if (x->name == name)
flag = false;
x = x->nextFile;
}
if (y != nullptr && y->name == name)
flag = false;
if (!flag)
{
cout<< "cannot create directory '" + name + "': File exists\n";
return;
}
File * newfile = new File{
name : name,
canRead : 1,
canWrite : 1,
frontFile : nullptr,
nextFile : nullptr,
parent : nowpath
};
if (nowpath->firstChildFile == nullptr)
nowpath->firstChildFile = nowpath->lastChildFile = newfile;
else
{
auto b = nowpath->lastChildFile;
b->nextFile = newfile;
newfile->frontFile = b;
nowpath->lastChildFile = newfile;
}
}
void rm(string name)
{
bool flag = false;
auto a = nowpath->firstChildFile;
auto b = nowpath->lastChildFile;
if (a == nullptr) goto cc;
if (a != nullptr && a->name == name)
{
flag = true;
bool fl = a == b ? true : false;
auto nxt = a->nextFile;
free(a);
nowpath->firstChildFile = nxt;
if (nxt != nullptr)
nxt->frontFile = nullptr;
if (fl)
nowpath->lastChildFile = nullptr;
}
else if (b != nullptr && b->name == name)
{
flag = true;
auto pre = b->frontFile;
free(b);
nowpath->lastChildFile = pre;
if (pre != nullptr)
pre->nextFile = nullptr;
}
else
{
a = a->nextFile;
while (a != nullptr && a != b && a->name != name)
a = a->nextFile;
if (a != nullptr && a->name == name)
{
flag = true;
auto pre = a->frontFile;
auto nxt = a->nextFile;
free(a);
if (pre != nullptr)
pre->nextFile = nxt;
if (nxt != nullptr)
nxt->frontFile = pre;
}
}
cc: if (flag)
cout<< "OK\n";
else cout<< "failed to remove '" + name + "' No such file\n";
}
void show_context(string name)
{
auto a = nowpath->firstChildFile;
auto b = nowpath->lastChildFile;
while (a != nullptr && a->name != name)
a = a->nextFile;
if (a == nullptr || a->name != name)
{
cout<< "No such file or directory\n";
return;
}
else cout<< a->context<< '\n';
}
void modify(string name)
{
auto a = nowpath->firstChildFile;
auto b = nowpath->lastChildFile;
while (a != nullptr && a->name != name)
a = a->nextFile;
if (a == nullptr || a->name != name)
{
cout<< "No such file or directory\n";
return;
}
cout<< "續(xù)寫 -r\n覆蓋 -w\n";
string op;
cin >>op;
if (op == "-w")
a->context = "";
cout<< "請輸入內(nèi)容\n";
getchar();
getline(cin,op);
a->context += op;
}
void show()
{
auto x = nowpath->firstChildFile;
while (x != nullptr)
{
cout<< x->name<< '\n';
x = x->nextFile;
}
auto xF = nowpath->firstChildFolder;
while (xF != nullptr)
{
cout<< xF->name<< '\n';
xF = xF->nextFolder;
}
}
void open_dir(string name)
{
if (name == "..")
{
if (nowpath != root)
nowpath = nowpath->parent;
return;
}
auto a = nowpath->firstChildFolder;
auto b = nowpath->lastChildFolder;
while (a != nullptr && a->name != name)
a = a->nextFolder;
if (a == nullptr || a->name != name)
{
cout<< "No such file or directory\n";
return;
}
nowpath = a;
}
void Operation(string op,string name)
{
if (op == "exit")
{
cout<< "bye\n";
exit(0);
}
if (op == "mkdir")
make_directory(name);
else if (op == "rmdir")
remove_directory(name);
else if (op == "touch")
touch_file(name);
else if (op == "rm")
rm(name);
else if (op == "cat")
show_context(name);
else if (op == "vim")
modify(name);
else if (op == "ls")
show();
else if (op == "cd")
open_dir(name);
else cout<< op + " : command not found\n";
}
int main()
{
createroot();
cout<< "創(chuàng)建目錄 -- mkdir\n\
刪除目錄 -- rmdir\n\
打開目錄 -- cd\n\
創(chuàng)建文件 -- touch\n\
刪除文件 -- rm\n\
查看文件 -- cat\n\
寫入文件 -- vim\n\
列出目錄 -- ls\n\
退出系統(tǒng) -- exit\n";
while (1)
{
cout<< getpath();
string s,op,name;
getline(cin,s);
stringstream ss(s);
ss >>op >>name;
Operation(op,name);
}
return 0;
}
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧