用java实现一些常见的Dos命令

DosTool.java 


package cn.dong.demo02;


import java.io.*;
import java.text.DateFormat;
import java.util.Date;

public class DosTool {
    /**
     * Dos的工具类
     */

    //显示目录下的文件或文件夹
    public static void dir(){
        Scanner.printf("请输入要查找的目录名:");
        String s = Scanner.getKey();
        File file = new File(s);
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.isDirectory()){
                System.out.println(f.getAbsolutePath()+"<DIR>");
            }
            if(f.isFile()){
                System.out.println(f.getAbsolutePath()+",文件大小为:"+f.length()/1024.0+"KB");
            }
        }
    }

    //创建文件夹
    public static void md(){
        Scanner.printf("请输入要创建的目录名:");
        String s = Scanner.getKey();
        File file = new File(s);
        if(file.exists()){
            System.out.println("该目录已经存在");
        }else{
            boolean b = file.mkdirs();
            if(b){
                System.out.println("创建成功");
            }else{
                System.out.println("创建失败");
            }
        }

    }

    //显示时间
    public static void time(){
        String ss = DateFormat.getTimeInstance().format(new Date());
        System.out.println(ss);
    }

    //显示日期
    public static void date(){
        String ss = DateFormat.getDateInstance().format(new Date());
        System.out.println(ss);
    }

    //对文件重命名
    public static void rename(){
        Scanner.printf("请输入重名命名的文件:");
        String sFile = Scanner.getKey();
        Scanner.printf("请输入修改后的文件名:");
        String dFile = Scanner.getKey();
        File f1 = new File(sFile);
        File f2 = new File(dFile);
        boolean b = f1.renameTo(f2);
        if(b){
            System.out.println("改名成功");
        }else{
            System.out.println("改名失败");
        }
    }

    //复制文件
    public static void copy(){
        Scanner.printf("请输入复制文件的位置:");
        String s = Scanner.getKey();
        Scanner.printf("请输入复制后的位置:");
        String d = Scanner.getKey();
        try {
            FileInputStream fis = new FileInputStream(s);
            FileOutputStream fos = new FileOutputStream(d);
            byte[] bs = new byte[1024];
            int c = 0;
            while((c=fis.read(bs))!=-1){
                fos.write(bs,0,c);
            }
            fis.close();
            fos.close();
            System.out.println("复制成功");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //退出
    public static void exit(){
        System.exit(0);
    }

    //运行程序
    public static void run(){
        Scanner.printf("请输入运行程序的命令:");
        String command = Scanner.getKey();
        Runtime r = Runtime.getRuntime();
        try {
            r.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //删除文件或文件夹
    public static void del(){
        Scanner.printf("请输入要删除的文件:");
        String s = Scanner.getKey();
        File file = new File(s);
        boolean b = file.delete();
        if(b){
            System.out.println("删除成功");
        }else{
            System.out.println("删除失败");
        }
    }


}

Scanner.java

package cn.dong.demo02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Scanner {
    /**
     * 键盘输入的方法
     */
    public static String getKey() {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = null;
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return s;
    }

    /**
     * 输入的方法
     * @param s
     */
    public static void printf(String s){
        System.out.println(s);
    }
}

JavaDos.java

package cn.dong.demo02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 用java实现Dos下的命令
 */
public class JavaDos {
    /**
     * 运行dos命令
     */
    public static void runDos(){
        Scanner.printf("------------欢迎来到dos界面--------------");
        while(true){
            Scanner.printf("请输入dos命令:");
            String s = Scanner.getKey();
            if(s.equalsIgnoreCase("dir")){
                DosTool.dir();
            }else if(s.equalsIgnoreCase("md")){
                DosTool.md();
            }else if(s.equalsIgnoreCase("time")){
                DosTool.time();
            }else if(s.equalsIgnoreCase("date")){
                DosTool.date();
            }else if(s.equalsIgnoreCase("rename")){
                DosTool.rename();
            }else if(s.equalsIgnoreCase("exit")){
                break;
            }else if (s.equalsIgnoreCase("copy")){
                DosTool.copy();
            }else if(s.equalsIgnoreCase("run")){
                DosTool.run();
            }else if(s.equalsIgnoreCase("del")){
                DosTool.del();
            }else{
                Scanner.printf("输入了非法命令");
            }

        }
        Scanner.printf("谢谢使用");

    }

    public static void main(String[] args) {
        runDos();
    }


}

运行结果:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值