一、所实现的功能
String LS = "ls"; //列出当前目录内容
String MKDIR = "mkdir"; //创建目录
String COPY = "copy"; //拷贝
String RM = "rm"; //删除
String CD = "cd"; //进入一个目录
String CDP = "cd.."; //进入上一层目录
二、架构
很多朋友可能会问为什么一个类就能写完的东西,要用那么多类来完成
①扩展性强
②结构清晰(主方法里 结构简单清晰不混乱)
③单里的设计模式,控制实例产生的数量,达到节约资源的目的
三、代码具体实现
ICmd接口(操作名称接口)
public interface ICmd {
String LS = "ls"; //列出当前目录内容
String MKDIR = "mkdir"; //创建目录
String COPY = "copy"; //拷贝
String RM = "rm"; //删除
String CD = "cd"; //进入一个目录
String CDP = "cd.."; //进入上一层目录
String[] COMMONDS = new String[]{LS,MKDIR,COPY,RM,CD,CDP};
}
ICommand接口(方法接口)
public interface ICommond {
/**
* 列出当前目录的所有内容 名字+size
*/
boolean list();
/**
* 创建一个目录
*/
boolean mkdir(String path);
/**
* 将src的文件复制到des位置
*/
boolean copy(String src, String des);
/**
* 删除文件
*/
boolean remove(String path);
/**
* 切换当前目录到子目录中
*/
boolean cd_to_child(String path);
/**
* 切换到上一层目录
*/
boolean cd_to_parent();
}
CommandTool类(方法的实现类)
public class CommondTool implements ICommond{
//默认请动起来操作的目录
private static final String DESKTOP_PATH = "D:\\Users\\Desktop";
//记录当前操作的目录路径
private StringBuilder currentDirPath;
public CommondTool() {
currentDirPath = new StringBuilder(DESKTOP_PATH);
}
//启动命令行工具
public void start(){
//创建读取指令的类的对象
CommondOperation operation = new CommondOperation();
//欢迎界面
System.out.println("欢迎使用Zc9741定制版命令行工具!");
while(true) {
//显示目录提示
showParent();
//输入指令
try {
operation.readCommond(this);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
private void showParent(){
System.out.print(currentDirPath+"->");
}
@Override
public boolean list() {
File[] files = FileManager.getInstance().list(currentDirPath.toString());
for (File file :files) {
//获取文件名
String name = file.getName();
//获取文件长度 字节 23.3kb 1KB = 1024byte
long size = file.length();
long kb = size / 1024;
long by = size % 1024;
System.out.print(name);
for (int i = 0; i < 40 - name.length(); i++){
System.out.print(" ");
}
System.out.println(kb+"."+by+"kb");
}
return false;
}
@Override
public boolean mkdir(String path) {
//
//拼接完整路径
String dirPath = currentDirPath.toString() + "\\" + path;
FileManager.getInstance().mkdir(dirPath);
return false;
}
@Override
public boolean copy(String src, String des) {
//拼接完整路径
String srcPath = currentDirPath.toString() + "\\" + src;
String desPath = currentDirPath.toString() + "\\" + des;
FileManager.getInstance().copy(srcPath,desPath);
return false;
}
@Override
public boolean remove(String path) {
//拼接完整路径
String dirPath = currentDirPath.toString() + "\\" + path;
FileManager.getInstance().remove(dirPath);
return false;
}
@Override
public boolean cd_to_child(String path) {
currentDirPath.append("\\"+path);
return false;
}
@Override
public boolean cd_to_parent() {
int start = currentDirPath.toString().lastIndexOf("\\");
int end = currentDirPath.length();
currentDirPath.delete(start,end);
return false;
}
}
CommandOperation类(接受用户输入and处理用户输入)
public class CommondOperation {
//回调对象
private ICommond listener;
//获取输入信息
private Scanner mScanner;
//保存所有指令
private List<String> commonds;
public CommondOperation() {
mScanner = new Scanner(System.in);
//将普通的Array类型转化为List
commonds = Arrays.asList(ICmd.COMMONDS);
}
//copy file1 file2
//next() 以空格作为结束符
//nextLine() 读取一行数据 以\n结尾
//接收用户输入的指令
public void readCommond(ICommond listener) throws PXDException.CommondNotExistException, PXDException.CommondArgumentErrorException {
this.listener = listener;
//接收指令
String commond = mScanner.nextLine();
//解析指令
parseCommond(commond);
}
//ls
//rm file
//copy file1 file2
private void parseCommond(String commond) throws PXDException.CommondNotExistException, PXDException.CommondArgumentErrorException {
//将指令以空格未分割符 分开
String[] componts = commond.split(" ");
//获取用户指令
String cmd = componts[0];
//判断指令是否存在
if (!commonds.contains(cmd)){
//输入的指令不存在
//抛出异常
throw new PXDException.CommondNotExistException("指令不存在");
}
//存在就解析是那种指令
switch (cmd){
case ICmd.CD:
//[cd file]
if (componts.length != 2){
throw new PXDException.CommondArgumentErrorException("cd 参数不正确");
}
listener.cd_to_child(componts[1]);
break;
case ICmd.CDP:
//cd..
if (componts.length != 1){
throw new PXDException.CommondArgumentErrorException("cd.. 不需要参数");
}
listener.cd_to_parent();
break;
case ICmd.MKDIR:
//mkdir file
if (componts.length != 2){
throw new PXDException.CommondArgumentErrorException("mkdir 参数不正确");
}
listener.mkdir(componts[1]);
break;
case ICmd.RM:
//rm file
if (componts.length != 2){
throw new PXDException.CommondArgumentErrorException("rm 参数不正确");
}
listener.remove(componts[1]);
break;
case ICmd.LS:
//ls
if (componts.length != 1){
throw new PXDException.CommondArgumentErrorException("ls 不需要参数");
}
listener.list();
break;
case ICmd.COPY:
//copy file1 file2
if (componts.length != 3){
throw new PXDException.CommondArgumentErrorException("copy 参数不正确");
}
listener.copy(componts[1],componts[2]);
break;
}
}
}
FileManager类(解决CommandTool中文件操作)
public class FileManager {
private static FileManager manager;
private FileManager(){}
//单例方法
public static FileManager getInstance(){
if (manager == null){
synchronized (FileManager.class){
if (manager == null){
manager = new FileManager();
}
}
}
return manager;
}
/**
* 创建目录
*/
public boolean mkdir(String path){
File file = new File(path);
if (file.exists()){
return false;
}
//deskTop/file/image/1.jpg
//mkdir要求路径中的目录都存在
//mkdirs如果路劲中的目录不存在 也会自动创建
return file.mkdir();
}
public boolean remove(String path){
File file = new File(path);
if (!file.exists()){
return false;
}
return file.delete();
}
/**
* 列出文件信息
*/
public File[] list(String path){
File file = new File(path);
if (!file.exists()){
return null;
}
return file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
if (s.startsWith(".")){
return false;
}
return true;
}
});
}
/**
* 文件或者目录的copy
*/
public boolean copy(String src,String des){
File srcFile = new File(src);
File desFile = new File(des);
//判断文件是否存在
if (!srcFile.exists() || desFile.exists()){
return false;
}
//判断是不是文件
if (srcFile.isFile()){
//直接copy文件
copyFile(src, des);
}else{
//是目录
//创建当前的目录
desFile.mkdir();
//需要将源目录的所有内容copy到des目录
for (File file: srcFile.listFiles()){
copy(file.getAbsolutePath(),des+"/"+file.getName());
}
}
return true;
}
private void copyFile(String src, String des){
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
//创建输入流
fis = new FileInputStream(src);
bis = new BufferedInputStream(fis);
//创建输出流
fos = new FileOutputStream(des);
bos = new BufferedOutputStream(fos);
//创建buffer
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1){
bos.write(buffer);
}
bos.flush();
}catch (Exception e){
e.printStackTrace();
}finally {
try {
bis.close();
fis.close();
bos.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
PXDException(自定义异常类—>抛出输入操作不正确异常)
public class PXDException {
/**
* 指令不存在
*/
static class CommondNotExistException extends Exception{
public CommondNotExistException(String s) {
super(s);
}
}
static class CommondArgumentErrorException extends Exception{
public CommondArgumentErrorException(String s) {
super(s);
}
}
}