描述
github源码啦取链接
git拉取: git@github.com:Coo0lCo/Command_System.git
可以取下载我的源码嘻嘻
这是最近写的一个文件管理系统 当然在这的文件指的是String类型字符串
这相当于一个雏形吧
底层实现原理
(1)数据结构我这里实现了一个双向的多叉树
(2)一个命令解析的封装类
(3)一个命令输入并对应实现操作的类
(4)一个main方法启动程序
结点数据存储结构
import java.util.LinkedList;
/**
* @author Doug Li Yong Jie
* --->结点的数据存储结构
*
*/
public class Node {
//储存孩子结点
public LinkedList<Node> children;
//指向双亲的指针
public Node pre;
//储存当前结点的数据
public String date;
//用于区分当前结点是为文件还是文件夹{文件-->0文件夹->1}
public int i;
public Node (){
this.date=null;
this.children=null;
this.pre=null;
this.i=1;
}
public Node(String date,Node pre,int i){
this.date=date;
this.i=i;
this.children=new LinkedList<>();
this.pre=pre;
}
}
命令解析
/**
* @author Li Yong Jie
* -->命令解析:
* 1.ls -l 罗列出当前结点下的所有文件
* 2.rm xxx 删除文件夹
* 3.rm -r xxx 删除文件
* 3.cd xxx 切换当前位置
* 4.mkdir xxx 创建文件夹
* 5.touch xxx 创建文件
* 6.whereis xxx 查找xxx的文件路径
* 7.pwd 显示当前路径
*/
public class command_parsing {
public String date;
public command_parsing(){
this.date=null;
}
/**
*
* @param command 待处理字符串
* @return 返回处理好的字符串
*/
private String strprocess(String command) {
String s=" ";
int indexEnd = command.length()-1;
for(int i = command.length()-1 ; i > -1 ; i--){
if(s.equals(command.charAt(i)+"")){
indexEnd = i;
break;
}
}
this.date=command.substring(indexEnd+1);
return command.substring(0,indexEnd);
}
//========================================================
public boolean ls_l(String command){
String s="ls -l";
return s.equals(command);
}
public boolean rmxxx(String command){
String s="rm";
return s.equals(strprocess(command));
}
public boolean rm_rxxx(String command){
String s="rm -r";
return s.equals(strprocess(command));
}
public boolean cdxxx(String command){
String s="cd";
return s.equals(strprocess(command));
}
public boolean cd__(String command){
String s="cd ..";
return s.equals(command);
}
public boolean mkdirxxx(String command){
String s="mkdir";
return s.equals(strprocess(command));
}
public boolean touchxxx(String command){
String s="touch";
return s.equals(strprocess(command));
}
public boolean whereisxxx(String command){
String s="whereis";
return s.equals(strprocess(command));
}
public boolean pwd(String command){
String s="pwd";
return s.equals(command);
}
}
接受用户输入命令并实现具体操作
import 算法and数据结构.数据结构.多叉树.文件管理系统.creatTree.creatTree;
import 算法and数据结构.数据结构.多叉树.文件管理系统.命令解析.command_parsing;
import java.util.Scanner;
/**
* @author Doug Li Yong Jie
* --->用户输出
* 以及输入内容的解析
* 判断是否为正确命令
* 做出相应回馈
* 注意!!!--->该操作是可重复允许的
*
*/
public class JudgeCommand{
public JudgeCommand(){
Scanner sc=new Scanner(System.in);
command_parsing command=new command_parsing();
creatTree creat=new creatTree();
while (true){
String commandString=sc.nextLine();
if ("".equals(commandString)){
System.out.println("Error: command not found: "+"");
continue;
}
if (command.cd__(commandString)){
creat.cd__();
}else if(command.cdxxx(commandString)){
creat.cd(command.date);
}else if (command.mkdirxxx(commandString)){
creat.mkdir(command.date);
}else if (command.touchxxx(commandString)){
creat.touch(command.date);
}else if (command.ls_l(commandString)){
creat.ls_l();
}else if (command.rmxxx(commandString)){
creat.rm(command.date);
}else if (command.rm_rxxx(commandString)){
creat.rm_r(command.date);
}else if (command.whereisxxx(commandString)){
creat.whereIs(command.date);
}else if (command.pwd(commandString)){
creat.pwd();
}else {
System.out.println("Error: command not found: "+commandString);
}
}
}
}
操作实现
import 算法and数据结构.数据结构.多叉树.文件管理系统.Node.Node;
import java.util.HashMap;
import java.util.Stack;
/**
* @author Doug Li Yong Jie
* --->操作实现
* 1.添加
* 2.删除
* 3.查找
* 4.路径--->遍历----->存储路径结点---->选取怎样的数据结构存储
*/
public class creatTree {
//树根
Node root;
//当前目录结点
Node thisNode;
//用栈存储路径 弹栈输出路径
Stack<Node> stack;
//违法命名哈希表
HashMap<String,Boolean> IllegalCommand;
public creatTree(){
//根结点
this.root=new Node("My Computer",null,1);
//创建一个栈
this.stack=new Stack<>();
//这里相当于把指向root的指针多添加了一个-->thisNode
this.thisNode=root;
root.pre=null;
IllegalCommand=new HashMap<>();
IllegalCommand.put("..",true);
thisNode.children.add(new Node("A",thisNode,1));
thisNode.children.add(new Node("B",thisNode,1));
thisNode.children.add(new Node("C",thisNode,1));
thisNode.children.add(new Node("D",thisNode,1));
}
public boolean isIllegalName(String Name){
if(IllegalCommand.get(Name)!=null){
return true;
}else {
return false;
}
}
/**
* cd xxx:切换文件夹
* @param date 待切换目录
*/
public void cd(String date){
for(int i = 0 ; i < thisNode.children.size() ;i++){
if(date.equals(thisNode.children.get(i).date)){
if(isFolder(thisNode.children.get(i))){
thisNode=thisNode.children.get(i);
System.out.println("已移动至-->"+date+" 文件夹下!");
}else {
System.out.println("cd: not a directory: "+date);
}
return;
}
}
System.out.println("cd: not a directory: "+date);
}
/**
* cd .. :返回上一个文件夹
*/
public void cd__(){
if (thisNode.pre!=null){
thisNode=thisNode.pre;
System.out.println("已返回上一级!");
}
}
/**
* ls -l:罗列当前为文件夹的文件或文件夹
*/
public void ls_l(){
if(root == thisNode){
System.out.print(root.date+"/ ");
}else {
SearchThisNode(thisNode);
}
System.out.print(" :");
for (int i = 0; i < thisNode.children.size(); i++) {
System.out.print(thisNode.children.get(i).date+", ");
}
System.out.println();
}
private void SearchThisNode(Node thisNode){
if(thisNode == null){
return;
}
stack.push(thisNode);
SearchThisNode(thisNode.pre);
pop(stack);
}
private void pop(Stack<Node> stack){
while(!stack.isEmpty()){
System.out.print(stack.pop().date+"/ ");
}
}
/**
* rm xxx:删除文件夹
* @param date 待删除文件夹
*/
public void rm(String date){
if (thisNode.children.size() == 0){
System.out.println("cd: not a directory: "+date);
return;
}
for (int i = 0; i < thisNode.children.size(); i++) {
if (date.equals(thisNode.children.get(i).date)){
if (isFolder(thisNode.children.get(i))){
thisNode.children.remove(i);
System.out.println("文件夹删除成功");
return;
}
System.out.println("该命令无法删除文件,请输入(rm -r 文件名) ,删除文件");
return;
}
}
System.out.println("cd: not a directory: "+date);
}
/**
* rm -r xxx:删除文件
* @param date 待删除文件
*/
public void rm_r(String date){
if (thisNode.children.size() == 0){
System.out.println("cd: not a directory: "+date);
return;
}
for (int i = 0; i < thisNode.children.size(); i++) {
if (date.equals(thisNode.children.get(i).date)){
if (isFolder(thisNode.children.get(i))){
System.out.println("该命令无法删除文件夹,请输入(rm 文件名) ,删除文件");
return;
}
thisNode.children.remove(i);
System.out.println("文件删除成功");
return;
}
}
System.out.println("cd: not a directory: "+date);
}
/**
* mkdir xxx:创建文件夹
* @param date 待创建文件夹
*/
public void mkdir(String date){
if(isIllegalName(date)){
System.out.println("非法的文件名");
return;
}
if (!isDuplicateName(thisNode,date)){
this.thisNode.children.add(new Node(date,thisNode,1));
System.out.println("文件夹创建成功!");
}else {
System.out.println("文件夹重名");
}
}
/**
* touch xxx:创建文件
* @param date 待创建文件
*/
public void touch(String date){
if(isIllegalName(date)){
System.out.println("非法的文件名");
return;
}
if (!isDuplicateName(thisNode,date)){
this.thisNode.children.add(new Node(date,thisNode,0));
System.out.println("文件添加成功!");
}else {
System.out.println("文件重名");
}
}
/**
* whereIs xxx:全局查找xxx的文件路径(可以是多个路径)
* @param date 待查找文件或者文件夹
*/
public void whereIs(String date){
SearchDate(root,date);
System.out.println();
}
private void SearchDate(Node root,String date){
if(root == null)return;
if(date.equals(root.date)){
SearchThisNode(root);
return;
}
for (int i = 0; i < root.children.size(); i++) {
SearchDate(root.children.get(i),date);
}
}
/**
* pwd:显示当前路径
*/
public void pwd(){
SearchThisNode(thisNode);
System.out.println();
}
/**
* isFolder 用于判断该结点是否为文件夹
* @param node 待判断结点
* @return true-->是文件夹 false-->是文件
*/
public boolean isFolder(Node node){
return node.i==1;
}
/**
* isDuplicateName 用于判断文件是否重名
* @param node 待查询文件的父亲
* @param Name 需要添加的文件或者文件夹名
* @return true-->文件重名 false-->文件微重名
*/
public boolean isDuplicateName(Node node,String Name){
if (node.children.size()==0)return false;
for(int i = 0 ; i < node.children.size() ;i++){
if(Name.equals(node.children.get(i).date)){
return true;
}
}
return false;
}
}
启动程序
import 算法and数据结构.数据结构.多叉树.文件管理系统.命令判断.JudgeCommand;
public class start {
public static void main(String[] args) {
new JudgeCommand();
}
}