Java 实现简单Shell 的demo

import java.io.*;
import java.lang.invoke.SwitchPoint;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class Echo{
    public void duty(String s){
        System.out.println(s);
    }
}

class Grep{
    public void duty(String pattern,File path) throws IOException {
         Reader reader=new FileReader(path);
        BufferedReader bufReader=new BufferedReader(reader);

        String s=null;
        while ((s=bufReader.readLine())!=null){
            Matcher m=Pattern.compile(pattern).matcher(s);
            if(m.find()){
                System.out.println(s);
            }
        }
        bufReader.close();
    }
}

class Pwd{
    public void duty(File path){
        System.out.println(path.getAbsoluteFile());
    }
}


class Ls{
    public void duty(File f){
        if (!f.exists()){
            System.out.println("no such directory");
        }
        String[] arr=f.list();
        int i=0;
        while (i<arr.length){
            System.out.println(arr[i]);
            i++;
        }
    }
}

class Cd{
    public File duty(File srcPath,File dstPath){
        if(!dstPath.exists()){
            System.out.println("No such file or directory");
            return srcPath;
        }else{
            return  dstPath;
        }
    }
}

class Cat{
    public void duty(File path) throws IOException {
        if (!path.exists()){
            System.out.println("no such file");
        }
        BufferedReader bufReader=new BufferedReader(new FileReader(path));
        String s=null;
        while ((s=bufReader.readLine())!=null){
            System.out.println(s);
        }
        bufReader.close();
    }
}

class Mkdir{
    public void duty(File path){
        path.mkdirs();
    }
}

class FindHome{
    public static File duty(File f) {
        if (f.getName().equals("home")){
            return f;
        }else{
            f=f.getParentFile();
            if (!f.exists()){
                return null;
            }
            return duty(f);
        }
    }
}


class Change{
    public static File duty(File file,String string){
        if (string.equals(".")){
            return file;
        }else if (string.equals("..")){
            return file.getParentFile();
        }else if ((string.equals("home") || string.equals("~"))){
            FindHome findHome=new FindHome();
            return findHome.duty(file);
        }else if (string.equals("/")){
            return new File("/");
        }else if(string.indexOf(0)=='/' && string.length()>=2){
			return new File(string);
		}else {
            return new File(file,string);
        }
    }
}






class Cp{
    public void duty(File src,File dst) throws IOException {
        FileInputStream fileInputStream=new FileInputStream(src);
        BufferedInputStream bufferedInputStream=new BufferedInputStream(fileInputStream);

        if (!dst.exists()){
            dst.createNewFile();
        }
        FileOutputStream fileOutputStream=new FileOutputStream(dst);
        BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(fileOutputStream);

        byte[] bytes=new byte[1024];
        int len=-1;

        while ((len=bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,len);
        }

        bufferedInputStream.close();
        bufferedOutputStream.close();
    }
}


public class Shell {
    public static String pathname;
    public static void main(String[] args) throws IOException {
        System.out.print("请输入你想选择的当前路径(例如 C:/User/wyh17):");
        Scanner scanner=new Scanner(System.in);
        pathname=scanner.nextLine();
        File f=new File(pathname);
        if (!f.exists()){
            System.out.println("选择路径错误.");
            main(args);
        }
        System.out.print("wyh@FX:"+f.getAbsolutePath()+"$");
        String str1;
        String[] arr;
        while (true){
            str1=scanner.nextLine();
            arr=str1.split(" ");
            switch (arr[0]){
                case "echo":
                    Echo echo=new Echo();
                    if (arr.length>2){
                        Grep grep=new Grep();
                        grep.duty(arr[1],new File(arr[4]));
                    }else {
                        echo.duty(arr[1]);
                    }
                    break;
                case  "grep":
                    Grep grep=new Grep();
                    grep.duty(arr[1],new File(arr[2]));
                    break;
                case "ls":
                    Ls ls=new Ls();
                    if (arr.length==1){
                        ls.duty(f);
                    }else{
                        Change change=new Change();
                        File file=new File(arr[1]);
                        ls.duty(file);
                    }
                    break;
                case "pwd":
                    Pwd pwd=new Pwd();
                    pwd.duty(f);
                    break;
                case "cd":
                    Cd cd=new Cd();
                    Change change=new Change();
                    File file=change.duty(f,arr[1]);
                    if (!file.exists()){
                        System.out.println("wrong destination");
                    }else {
                        f=cd.duty(f,file);
                    }
                    break;
                case "cat":
                    Cat cat=new Cat();
                    File file1=new File(f,arr[1]);
                    cat.duty(file1);
                    break;
                case "mkdir":
                    Mkdir mkdir=new Mkdir();
                    File file2=new File(f,arr[1]);
                    mkdir.duty(file2);
                    break;
                case "cp":
                    Cp cp=new Cp();
                    cp.duty(new File(arr[1]),new File(arr[2]));
                    break;
                case "exit":
                    System.exit(0);
                default:
                    System.out.println("no such command");
            }
            System.out.print("wyh@FX:"+f.getAbsolutePath()+"$");

        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值