模拟CMD操作文件(夹)

在控制台模拟操作cmd

cmd一般操作界面

我们设计简单的程序实现以下功能

1.cd显示当前目录的名称或将其更改。

2.date显示时间

3.md 创建一个目录

4. rd 删除目录

5.dir 显示一个目录中的文件和子目录

6 help 提示操作

 

代码

先在项目下创建一个help.txt文件,内容从cmd的help中拷贝。

 

package com.nll.io;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class Cmd {
public static void main(String[] args) {
    System.out.println("************************************");
    System.out.println("****************nll*****************************");
    System.out.println("****************@2020****************");
    System.out.println("*************************************");
    //先来显示我们有哪些盘
    showRoot();
    
    //我们给一个默认的路径 我们默认的是第一个盘
    String path=File.listRoots()[0].getPath();

    Scanner sc=new Scanner(System.in);
    //开始循环的来输入指令
    while(true) {
        //首先 先展示
        System.out.print(path+">");//这个>没有其他意思,主要是为了和cmd一致
        
        String  cmd=sc.nextLine();
        //开始判断指令
        if("help".equalsIgnoreCase(cmd)) {
            help();
        }else if(cmd.endsWith(":")) {
            path=changeDisk(cmd,path);
            System.out.println();
        }else if("dir".equalsIgnoreCase(cmd)) {
            dir(path);
        }else if(cmd.startsWith("cd ")) {
            path=cd(cmd,path);
        }else if(cmd.startsWith("copy ")) {
            copy(cmd,path);
        }else if(cmd.startsWith("del ")) {
            del(cmd,path);
        }else if(cmd.startsWith("md ")){
            md(cmd,path);
        }else if(cmd.startsWith("rd")) {
            rd(cmd,path);
        }else if(cmd.equals("date")) {
            Calendar c=Calendar.getInstance();
            Date d=new Date();
             int i=(c.get(Calendar.DAY_OF_WEEK))-1;
            
            SimpleDateFormat sdf=new SimpleDateFormat("YYYY/MM/dd"+"\t"+(i==0?"日":"i"));
            System.out.println("当前日期:"+sdf.format(d));
        }else {
            System.out.println("'"+cmd+"'不是内部或外部命令,也不是可运行的程序\n");
        }
        
    }
    
}
private static void rd(String cmd, String path) {
    // TODO Auto-generated method stub
    String c=cmd.substring(3).trim();
    File dir=new File(path+"\\"+c);
    deleleAll(dir);
    System.out.println("删除成功!");
}
private static void deleleAll(File f) {
    // TODO Auto-generated method stub
    // TODO Auto-generated method stub
    //首先判断 你这个f是文件还是目录
    if(f.isFile()) {
        //如果是文件 则直接删除
        f.delete();
        return;
    }
    //目录 则获取到所有文件
    File[] fs=f.listFiles();
    for(File file:fs) {
        deleleAll(file);//递归自己调用自己
    }
    f.delete();//删除目录
}
private static void md(String cmd, String path) {
    // TODO Auto-generated method stub
    String c=cmd.substring(3).trim();
    File dir=new File(path+"\\"+c);
    if(!dir.exists()) {
        dir.mkdir(); 
        System.out.println(c+"创建成功");
    }else {
        System.out.println("创建失败");
    }
    
}
private static void del(String cmd, String path) {
    // TODO Auto-generated method stub
    String c=cmd.substring(4).trim();
    File f=new File(path,c);
    if(f.exists()&&f.isFile())
    {
    File f2=new File(path+"\\"+c);
    f2.delete();
    }else {
        System.out.println("此文件不存在或不可删除");
    }
}
private static void copy(String cmd, String path) {
    // TODO Auto-generated method stub
    String [] arr=cmd.split(" ");
     File f1=new File(path,arr[1]);
     File f2=new File(path,arr[2]);
     
     if(f1.exists()&&f2.exists()&&f1.isFile()&&f2.isFile()) {
         copy1(arr[1],arr[2],path);
     }
     
}
private static void copy1(String a1, String a2, String path) {
    // TODO Auto-generated method stub
    File f1=new File(path+"\\"+a1);
    File f2=new File(path+"\\"+a2);
    try {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(f1));
        byte [] b=new byte[bis.available()];
        bis.read(b);
        bis.close();
        OutputStream os=new FileOutputStream(f2);
        os.write(b);
        os.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
private static String cd(String cmd, String path) {
    String newPath=cmd.substring(3).trim();
    if(".".equalsIgnoreCase(newPath)) {
        return path;
        
    }else if("..".equalsIgnoreCase(newPath)) {
        //返回上一级
        File f=new File(path);
        String parents=f.getParent();
        if(parents!=null) {
            return parents;
        }else {
            return path;
        }
    }else {
        //切换文件夹
        
        File file=new File(path,newPath);
        if(file.exists()&&file.isDirectory()) {
            return file.getPath();
            
        }else {
            //没有
            System.out.print("系统找不到指定的路径。\n");
            return path;
        }
    }
}
private static void dir(String path) {
    // TODO Auto-generated method stub
    //先根据当前目录 来获取到所有的子文件
    File file=new File(path);
    File [] fs=file.listFiles();
    int fnum=0;
    int dnum=0;
    
    System.out.print("\n");
    for(File f:fs) {
        long time=f.lastModified();
        Date d=new Date(time);
        SimpleDateFormat sdf=new SimpleDateFormat("YYYY年MM月dd日   HH:mm");
        System.out.print(sdf.format(d));
        //接着判断是否是目录
        if(f.isDirectory()) {
            dnum++;
            System.out.print("\t目录\t\t");
        }else {
            fnum++;
            if(f.length()>10000000)
            {
                System.out.print("\t\t"+f.length());
            }else {
                System.out.print("\t\t"+f.length()+"\t");
            }
            
        }
    
    System.out.print("\t"+f.getName()+"\n");
    }
    System.out.print("\n总共"+fnum+"个文件,总共"+dnum+"个目录\n");
}
private static String changeDisk(String cmd, String path) {
    if(new File(cmd).exists()) {
        return cmd.toUpperCase()+"\\";
    }else {
        System.out.print("系统找不到指定的驱动器。\n");
        return path;
    }
    
}

//help指令的方法
private static void help() {
    // TODO Auto-generated method stub
    try {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("help.txt"));
        byte[] b =new byte[bis.available()];
        bis.read(b);
        //读出来之后显示
        String str=new String(b);
        System.out.println(str+"\n");
        //流使用完之后 避免浪费内存 记得关闭流
        bis.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

private static void showRoot() {
    // TODO Auto-generated method stub
    File[] fs=File.listRoots();
    for(File f:fs) {
        System.out.print(f.getPath()+"\t");
    }
    System.out.println("\n\n");
}
}

 

效果如下

 

posted @ 2020-04-15 20:53  小嗨皮皮  阅读(653)  评论(0编辑  收藏  举报