zoukankan      html  css  js  c++  java
  • 模拟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"+"	"+(i==0?"日":"i"));
                System.out.println("当前日期:"+sdf.format(d));
            }else {
                System.out.println("'"+cmd+"'不是内部或外部命令,也不是可运行的程序
    ");
            }
            
        }
        
    }
    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("系统找不到指定的路径。
    ");
                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("
    ");
        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("	目录		");
            }else {
                fnum++;
                if(f.length()>10000000)
                {
                    System.out.print("		"+f.length());
                }else {
                    System.out.print("		"+f.length()+"	");
                }
                
            }
        
        System.out.print("	"+f.getName()+"
    ");
        }
        System.out.print("
    总共"+fnum+"个文件,总共"+dnum+"个目录
    ");
    }
    private static String changeDisk(String cmd, String path) {
        if(new File(cmd).exists()) {
            return cmd.toUpperCase()+"\";
        }else {
            System.out.print("系统找不到指定的驱动器。
    ");
            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+"
    ");
            //流使用完之后 避免浪费内存 记得关闭流
            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()+"	");
        }
        System.out.println("
    
    ");
    }
    }

    效果如下

  • 相关阅读:
    数据库悲观锁与乐观锁
    windows消息大全
    Ubuntu 16.04 LTS GNOME版本下载
    Spring MVC-表单(Form)处理示例(转载实践)
    Spring MVC-Hello World示例(转载实践)
    Spring MVC-环境设置(转载实践)
    MyBatis在注解上使用动态SQL(@select使用if)
    Ubuntu 16.04安装Ubuntu After Install工具实现常用软件批量安装
    Ubuntu 16.04开机自动挂载硬盘分区(转)
    Spring MVC-概述(转载实践)
  • 原文地址:https://www.cnblogs.com/ll-hb/p/12687358.html
Copyright © 2011-2022 走看看