Java实现常见的dos命令

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/*
    常见的dos命令
    dir 列文件名
    exit 退出cmd.exe程序或目前
    help [命令] 或 ?[命令] 查看命令说明
    copy 拷贝文件
    del 删除文件
    type 显示文件内容
    del 删除文件
    comp比较两个或两套文件的内容
 */
public class CMDDemo01 {
    private static String [] cmd={"dir","copy","del","type","comp","help"};
    public static void main(String[] args) {
        System.out.println("关于dos命令及其作用");
        System.out.println("1.dir 列文件名");
        System.out.println("2.copy 拷贝文件");
        System.out.println("3.del 删除文件");
        System.out.println("4.type 显示文件内容");
        System.out.println("5.comp比较两个或两套文件的内容");
        System.out.println("6.help [命令] 或 ?[命令] 查看命令说明");
        System.out.println("7.exit 退出cmd.exe程序或目前");
        //从键盘上获取一个字符串
        Scanner sc=new Scanner(System.in);
        while (true){
            System.out.print(":");
            String s=sc.nextLine();
            //判断输入的是否为exit
            if(s.equals("exit")){
                System.exit(0);
            }
            //将字符串分割成字符数组
            String[] split = s.split(" ");
            int n=findCmd(split[0]);
            if(n!=-1) {
                switch (n) {
                    case 0:
                        doDir(split);
                        break;
                    case 1:
                        doCopy(split);
                        break;
                    case 2:
                        doDel(split);
                        break;
                    case 3:
                        doType(split);
                        break;
                    case 4:
                        doComp(split);
                        break;
                    case 5:
                        doHelp();
                        break;
                    default:
                }
            }else {
                System.out.println("您输入的命令暂不支持");
            }
        }
    }

    public static int findCmd(String s){
        int index=-1;
        for(int i=0;i<cmd.length;i++){
            if(s.equalsIgnoreCase(cmd[i])){//忽略大小写的影响
                index=i;
                break;
            }
        }
        return index;
    }

    // dir 列文件名
    public static void doDir(String [] s){
        String name=null;
        if(s.length==1){
            name=".";
        }else {
            name=s[1];
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd  HH:mm");
        File file=new File(name);
        int count =0;
        //判断该文件是否为文件夹
        if(file.isDirectory()){//如果是文件,获取该文件
            String[] listName = file.list();
            for(int i=0;i<listName.length;i++){
                //创建新的文件
                File f=new File(name+"/"+listName[i]);//
                Date d=new Date(f.lastModified());//lastModified()表示该文件最后编辑的时间
                System.out.println(sdf.format(d));
                if(f.isFile()){
                    System.out.print("\t\t");
                    System.out.println(listName[i]);
                    count++;
                }else {
                    System.out.print("\t<DIR>\t");
                    System.out.println("\t"+listName[i]);
                }
            }
        }else {
            count++;
            Date d1=new Date(file.lastModified());
            System.out.print(sdf.format(d1));
            System.out.print("\t\t");
            System.out.println(file.getName());
        }
        System.out.println("文件共有"+count+"个");
    }

    //copy 拷贝文件
    public static void doCopy(String [] s){
        int index=0;
        if(s.length!=3){
            System.out.println("该命令格式输入错误");
            index=0;
        }else {
            //获取想要复制文件的目的地和复制文件的路径
//            Path path1 = Paths.get(s[1]);
//            Path path2 = Paths.get(s[2]);
//            //用Files中的复制方法来复制文件
//            try {
//                Files.copy(path1,path2, StandardCopyOption.REPLACE_EXISTING);
//            }catch(IOException e){
//                e.printStackTrace();
//            }

            //根据键盘输入的文件地址创建File对象
            File f1=new File(s[1]);
            File f2=new File(s[2]);
            //用字节流来复制文件(夹)
            try {
                copyFolder(f1,f2);
            } catch (IOException e) {
                e.printStackTrace();
            }
            index=1;
        }
        if(index==1) {
            System.out.println("文件复制成功");
        }else {
            System.out.println("文件复制失败!");
        }
    }

    //复制文件的方法(通过字节缓冲输入流来实现文件的复制)
    private static void copyFile(File srcFile,File destFile)throws IOException {
        //创建字节缓冲输入流,读数据
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcFile));

        //创建字符缓冲输出流写数据
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(destFile));

        //读取数据(一次读取一个字节数组)
        byte[]bys=new byte[1024];
        int len;
        while ((len= bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }

    //文件夹的复制(由于文件夹里有其他东西,所以只能用字节流复制,因为字节缓冲流比字节流快,所以用字节缓冲流)
    private static void copyFolder(File srcFile, File destFile) throws IOException {
        //判断数据源File是否是目录
        if(srcFile.isDirectory()) {
            //在目的地下创建和数据源File名称一样的目录
            String srcFileName = srcFile.getName();
            File newFolder = new File(destFile,srcFileName); //F:\\itcast
            if(!newFolder.exists()) {
                newFolder.mkdir();
            }
            //获取数据源File下所有文件或者目录的File数组
            File[] fileArray = srcFile.listFiles();
            //遍历该File数组,得到每一个File对象
            for(File file : fileArray) {
                //把该File作为数据源File对象,递归调用复制文件夹的方法
                copyFolder(file,newFolder);
            }
        } else {
            //说明是文件,直接复制,用字节流
            File newFile = new File(destFile,srcFile.getName());
            copyFile(srcFile,newFile);
        }
    }

    //del 删除文件
    public static void doDel(String [] s){
        int index=0;
        if(s.length!=2){
            System.out.println("请输入正确的格式");
            index=0;
        }else {
            //调用Paths中的get方法获取到文件的绝对路径
            Path source=Paths.get(s[1]);
            try {
                Files.deleteIfExists(source);
            }catch(IOException e){
                e.printStackTrace();
            }
            index=1;
        }
        //判断文件是否删除成功后
        if(index==0){
            System.out.println("文件删除失败");
        }else {
            System.out.println("文件删除成功");
        }
    }

    //type 显示文件内容
    public static void doType(String [] s) {
        BufferedReader br = null;
        FileReader fr = null;
        if (s.length != 2) {
            System.out.println("输入格式错误");
            System.out.println("type: 显示文件内容");
        } else {
            try {
                //创建字符缓冲输入流
                String line;
                fr = new FileReader(s[1]);
                br = new BufferedReader(fr);
                while ((line=br.readLine())!=null) {
                    System.out.println(line);
                }
            } catch (FileNotFoundException e) {
                System.out.println();
            } catch (IOException ioe) {
                System.out.println("请输入文件地址");
            } finally {
                try {
                    //释放资源
                    fr.close();
                    br.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //comp比较两个或两套文件的内容
    public static void doComp(String [] s){
        if(s.length!=3){
            System.out.println("请您输入正确的格式");
        }else {
            //先判断您输入的文件是否存在

            //创建两个File对象
            File f1=new File(s[1]);
            File f2=new File(s[2]);

            int index=0;//用来判断是否要比较文件内容
            //判断第一个文件是否存在
            if(!f1.exists()){
                System.out.println("您所输入的文件"+f1.toString()+"不存在");
            }
            //判断第二个文件是否存在
            if(!f2.exists()){
                System.out.println("您所输入的文件"+f2.toString()+"不存在");
            }
            if(f1.exists()&&f2.exists()){//只有两个文件同时存在才能比较其内容
                index=1;
            }
            if(index==1){
                try {
                    //创建两个字节缓冲流,读取两个文件的内容
                    BufferedInputStream bis1=new BufferedInputStream(new FileInputStream(s[1]));
                    BufferedInputStream bis2=new BufferedInputStream(new FileInputStream(s[2]));

                    int n1,n2;
                    int n=0;
                    while (((n1=bis1.read())!=-1)&&((n2=bis2.read())!=-1)){//一一比较两个文件中的任何一个字节
                        if(n1==n2){
                            n=1;//遍历文件的所有内容
                        }else {
                            n=0;//只要有一个字节不一样,文件就不相同
                        }
                    }
                    if(n==1){
                        System.out.println("文件相同");
                    }else {
                        System.out.println("文件不同");
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                System.out.println("文件不同");
            }
        }
    }

    //help [命令] 或 ?[命令] 查看命令说明
    public static void doHelp(){
        System.out.println("目前只支持以下的几种命令");
        //遍历数组,输出所有命令的内容
        for(String s:cmd){
            System.out.print(s+" ");
        }
        System.out.println(" ");
    }

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我要学好编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值