package package1;
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,String path) throws IOException {
FileReader 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);
}
}
}
}
class Pwd{
public void duty(String path){
File f=new File(path);
System.out.println(f.getAbsoluteFile());
}
}
class Ls{
public void duty(File f){
String[] arr=f.list();
int i=0;
while (i<arr.length){
System.out.println(arr[i]);
i++;
}
}
}
class Cd{
public File duty(File srcPath,String dstPath){
File f=new File(dstPath);
if(!f.exists()){
System.out.println("No such file or directory");
return srcPath;
}else{
return f;
}
}
}
class Cat{
public void duty(File path,String name) throws IOException {
File f=new File(path,name);
if (!f.exists()){
System.out.println("no such file");
}
BufferedReader bufReader=new BufferedReader(new FileReader(f));
String s=null;
while ((s=bufReader.readLine())!=null){
System.out.println(s);
}
}
}
class Mkdir{
public void duty(File path,String name){
File f=new File(path,name);
f.mkdirs();
}
}
class Cp{
public void duty(String src,String dst) throws IOException {
FileInputStream fsrc=new FileInputStream(src);
byte[] arr=new byte[fsrc.available()];
fsrc.read(arr);
fsrc.close();
File fdst=new File(dst);
if (!fdst.exists()){
fdst.createNewFile();
}
FileOutputStream fds=new FileOutputStream(fdst);
fds.write(arr);
fds.close();
}
}
public class Shell {
public static String pathname;
public static void main(String[] args) throws IOException {
System.out.println("请输入你想选择的当前路径(例如 C:/User/wyh17):");
Scanner scanner=new Scanner(System.in);
pathname=scanner.nextLine();
File f=new File(pathname);
if (!f.exists()){
f.mkdirs();
}
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],arr[4]);
}else {
echo.duty(arr[1]);
}
break;
case "grep":
Grep grep=new Grep();
grep.duty(arr[1],arr[2]);
break;
case "ls":
Ls ls=new Ls();
ls.duty(f);
break;
case "pwd":
Pwd pwd=new Pwd();
pwd.duty(pathname);
break;
case "cd":
Cd cd=new Cd();
f=cd.duty(f,arr[1]);
break;
case "cat":
Cat cat=new Cat();
cat.duty(f,arr[1]);
break;
case "mkdir":
Mkdir mkdir=new Mkdir();
mkdir.duty(f,arr[1]);
break;
case "cp":
Cp cp=new Cp();
cp.duty(arr[1],arr[2]);
break;
case "exit":
System.exit(0);
default:
System.out.println("no such command");
}
System.out.print("wyh@FX:"+f.getAbsolutePath()+"$");
}
}
}
来源:CSDN
作者:飔下独角兽
链接:https://blog.csdn.net/weixin_44625793/article/details/104171037