FileSystemDOS
这是一个基于java的简单的仿DOS文件管理系统
使用了java中File的
实现的功能
- 实现
cd
命令 ,支持参数选项 - 实现
mkdir
命令,支持参数选项 - 实现
copy
复制命令,支持参数选项 - 实现
del
删除命令,支持参数选项 - 实现
dir
列出当前路径的子目录和文件,支持参数选项 - 实现
type
查看文件
大致编写的流程
- 确定命令接口Command
- 编写帮助Help
- 写FileController控制流程
- 对着流程一个一个实现命令
- 先编写每个命令的help方法
- 然后在编写excute方法
- 初步测试
- 最后进行简单测试
定义接口Command
/**
* @time 2019年8月6日
* @instruction
* 命令
*/
public interface Command {
/**
* 命令未找到
*/
final int NOTFINDCOMMAND = -1;
/**
* 参数未找到
*/
final int NOTFINDSECONDE = -2;
/**
* 语法异常
*/
final int ERRORCOMMAND = -3;
/**
* 执行成功
*/
final int SUCCESS = 1;
/**
* 未完成
*/
final int UNDONE = 0;
/**
* 不可读
*/
final int DONTREAD = 2;
/**
* 执行出错
*/
final int ERROR = 3;
/**
* 不是目录
*/
final int NOTADIRECTORY = 4;
/**
* 执行命令
*/
int excute(String[] command);
/**
* 命令的帮助
*/
void help();
}
定义了接口状态常量,每个命令都有的执行方法和帮助方法
ps:接口中的命令执行状态常量是逐渐添加的,编写具体命令时需要用到的状态然后再添加
帮助命令Help
/**
* @time 2019年8月9日
* @instruction
* 帮助命令
*/
public class Help implements Command {
public int excute(String[] command) {
//设置状态为未完成
int i = Command.UNDONE;
//用户只输入help,给出帮助概述
if(command.length == 1) {
help();
i = Command.SUCCESS;
}else if(command.length > 2){
//如果参数多于两个,那么命令错误
i = Command.ERRORCOMMAND;
}else {
//传入,执行对应命令
switch(command[1]) {
case "cd":
new Cd().help();
break;
case "dir":
new Dir().help();
break;
case "mkdir":
new Mkdir().help();
break;
case "copy":
new Copy().help();
break;
case "del":
new Del().help();
break;
case "type":
new Type().help();
break;
case "help":
help();
default:
i = Command.NOTFINDSECONDE;
return i;
}
//最后执行成功
i = Command.SUCCESS;
}
return i;
}
/**
* 帮助文档的概述
*/
public void help() {
StringBuilder str = new StringBuilder();
str.append("-------------------------帮助-------------------------\n");
str.append("命令格式:\n");
str.append("命令名称 [参数][参数]\n\n");
str.append("cd \t").append("显示当前目录名或改变当前目录。\n");
str.append("dir \t").append("显示目录中的文件和子目录列表。\n");
str.append("mkdir \t").append("创建目录\n");
str.append("copy \t").append("将一份或多份文件复制到另一个位置。\n");
str.append("del \t").append("删除一个或数个文件。\n");
str.append("type \t").append("显示文本文件的内容\n");
str.append("exit \t").append("退出\n");
str.append("若想查看详细命令,请键入: \"help 命令名称\" 查看 \n");
System.out.println(str);
}
}
先大致的写了帮助概述,然后照着帮助把具体的每一条命令的对应文件建立出来(他们都实现了Command接口),最后写excute方法
控制流程FileController
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.Map;
import java.util.Scanner;
import com.command.Command;
import com.command.impl.Cd;
import com.command.impl.Copy;
import com.command.impl.Del;
import com.command.impl.Dir;
import com.command.impl.Help;
import com.command.impl.Mkdir;
import com.command.impl.Type;
import com.utils.Init;
public class FileController {
/**
* 当前基本路径
*/
public static String basePath;
/**
* 当前用户
*/
static String user;
/**
* 当前执行的命令
*/
Command CurrentCommand;
Scanner in = new Scanner(System.in);
public FileController() {
//初始化,读配置文件
basePath = Init.path;
user = Init.user;
CurrentCommand = null;
}
public void start() {
System.out.println("欢迎来到 DosfileSystem! 如果需要帮助,请键入 help");
controller();
}
/**
* 格式化命令输入
* @return
*/
public String[] getCinFormat() {
System.out.print(basePath + " #" + user + "#:");
String parse = in.nextLine(); //输入
String[] command = parse.trim().split(" ");
command[0] = command[0].toLowerCase();//全部转换到小写
return command;
}
/**
* 控制中枢
*/
public void controller() {
while(true) {
String[] command = getCinFormat();
switch(command[0]) {
case "cd":
CurrentCommand = new Cd();
break;
case "dir":
CurrentCommand = new Dir();
break;
case "mkdir":
CurrentCommand = new Mkdir();
break;
case "copy":
CurrentCommand = new Copy();
break;
case "del":
CurrentCommand = new Del();
break;
case "type":
CurrentCommand = new Type();
break;
case "exit":
System.exit(0);//退出
case "help":
CurrentCommand = new Help();
break;
default:
System.<