利用java实现shell部分命令--不利用runtime类

完成了一个小任务,把代码全贴上来
作为一个纯java,linux小白,我们的第一个任务就是利用java实现部分shell命令,这个任务帮助自己理解了java语言和linux系统
没想到自己整合过后一共也有四百行了,过程蛮辛酸的,在文件路径的地方鼓捣了好久一直失败
整个代码最重要的思路就是
方法一定要通过传入file文件的绝对路径来写
否则程序会被困在工程路径中,无法跳转其他目录


cat.java

package ezshell;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class cat {

	public void main(String str,File file) throws IOException {
		// TODO Auto-generated method stub
		String nowlocal=file.getAbsolutePath();
		nowlocal=nowlocal.concat("/"+str);
	    file=new File(nowlocal);
		if(file.exists()) {
		BufferedReader buf=new BufferedReader(new FileReader(file));
		String strs=null;
		while((strs=buf.readLine())!=null) {
			System.out.println(strs);
		}
		buf.close();
		}else {
			System.out.println(str+":没有那个文件和目录");
		}
	}

}


cp.java

package ezshell;

import java.io.*;
import java.nio.channels.FileChannel;

public class cp {
	@SuppressWarnings("resource")
	public static void copy(File source,File dest) throws IOException{
		FileChannel inputchan=null;
		FileChannel outputchan=null;
		try {
			inputchan=new FileInputStream(source).getChannel();
			outputchan=new FileOutputStream(dest).getChannel();
			outputchan.transferFrom(inputchan, 0, inputchan.size());
			}
		finally {
			inputchan.close();
		    outputchan.close();
		    }
	}

	public void main(String str1,String str2,File file) throws IOException {
		// TODO Auto-generated method stub
		String nowlocal1=file.getAbsolutePath();
		nowlocal1=nowlocal1.concat("/"+str1);
	    file=new File(nowlocal1);
		String nowlocal2=file.getAbsolutePath();
		nowlocal2=nowlocal2.concat("/"+str2);
	    file=new File(nowlocal2);
		File nofile=new File(str1);
		File copyfile=new File(str2);
		copy(nofile,copyfile);
	}

}

	public void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("请输入被复制文件file"+'\n'+"/home/victor/eclipse-workspace/ezshell/content");
		Scanner sc1=new Scanner(System.in);
		String str1=sc1.nextLine();
		System.out.println("请输入复制到的文件copy"+'\n'+"/home/victor/eclipse-workspace/ezshell/copy");
		Scanner sc2=new Scanner(System.in);
		String str2=sc2.nextLine();
		File file=new File(str1);
		File copyfile=new File(str2);
		copy(file,copyfile);
		sc1.close();
		sc2.close();
	}

}

echo.java

package ezshell;

import java.io.IOException;
public class echo {

	public void main(String str) throws IOException{
		// TODO Auto-generated method stub
		System.out.println(str);
	}

}


echogrep.java

package ezshell;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class echogrep {

	public void main(String str1,String str2) throws IOException{
		// TODO Auto-generated method stub
		String res=str1.replaceAll("\\\\n","\n");
		File file=new File("/home/victor/eclipse-workspace/ezshell/StringWrite");
		BufferedWriter buw=new BufferedWriter(new FileWriter(file));
		buw.write(res.replaceAll("\"", ""));
		buw.close();
		BufferedReader buf=new BufferedReader(new FileReader(file));
		Pattern par=Pattern.compile(str2.replaceAll("\"", ""));
		//System.out.print(par);
		String line=null;
		while((line=buf.readLine())!=null)
		{
			Matcher mat=par.matcher(line);
			if(mat.find())
			{
				System.out.println(line);
			}
		}
		buf.close();
		}

}


grep.java

package ezshell;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class grep {

	public void main(String str1,String str2,File file) throws IOException{
		// TODO Auto-generated method stub
		String nowlocal=file.getAbsolutePath();
		nowlocal=nowlocal.concat("/"+str2);
	    file=new File(nowlocal);
		if(file.exists()) {
		    BufferedReader btf=new BufferedReader(new FileReader(str2));
		    String ser=str1.replaceAll("\"","");
		    Pattern par=Pattern.compile(ser);
		    String line=null;
		    while((line=btf.readLine())!=null) {
			    Matcher mat=par.matcher(line);
			    if(mat.lookingAt()) {
				    System.out.println(line);
			    }
		    }
		    btf.close();
	    }else{
		System.out.println("找不到文件");
	}
	}

}


ls.java

package ezshell;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

public class ls {
	public List<String> dirListrec(String dir){//递归
		List<String> list=new LinkedList<String>();
		File file=new File(dir);
		list.add(file.getAbsolutePath());
		File[] listfile1=file.listFiles();
		if(listfile1!=null&&listfile1.length!=0)
		{
			for(File f:listfile1) {
				/*if(f.isDirectory()) {
					List<String> dirListrec=dirListrec(f.getAbsolutePath());
					list.addAll(dirListrec);
					continue;
				}*/
				list.add(f.getAbsolutePath());
			}
		}
		return list;
	}
	/*
	public List<String> dirListsta(String dir){//栈
		List<String> list=new LinkedList<String>();
		Stack<File> stack=new Stack<File>();
		File file=new File(dir);
		list.add(file.getAbsolutePath());
		stack.push(file);
		//list.add(file.getAbsolutePath());
		while(!stack.empty()) {
			File popfile=stack.pop();
			File[] listfile2=popfile.listFiles();
			if(listfile2==null||listfile2.length==0) continue;
			for(File f:listfile2) {
				if(f.isDirectory()) {
					stack.push(f);
				}
				list.add(f.getAbsolutePath());
			}
		}
		return list;
		
	}
	public List<String> dirListque(String dir){//队列
		List<String> list=new LinkedList<String>();
		Queue<File> queue=new LinkedBlockingQueue<File>();
		File file=new File(dir);
		list.add(file.getAbsolutePath());
		queue.add(file);
		while(!queue.isEmpty()) {
			File outfile=queue.remove();
			File[] listfile3=outfile.listFiles();
			if(listfile3==null||listfile3.length==0) continue;
			for(File f:listfile3) {
				if(f.isDirectory()) {
					queue.add(f);
				}
				list.add(f.getAbsolutePath());
			}
		}
		return list;
	}*/
	public void main(File file) {
		// TODO Auto-generated method stub
		ls l=new ls();
		List<String> reclist=l.dirListrec(file.getAbsolutePath());
		for(String str:reclist) {
			str=str.replaceAll(file.getAbsolutePath(),"");
			str=str.replaceAll("/","");
			System.out.println(str);
		}
		//List<String> stalist=l.dirListsta(dir);
		//List<String> quelist=l.dirListque(dir);
		//System.out.println("栈搜索打印当前目录下所有文件");
		//printlist(stalist);	
		//System.out.println("队列搜索打印当前目录下所有文件");
		//printlist(quelist);

	}
	

}


mkdir.java

package ezshell;

import java.io.File;

public class mkdir {

	public void main(String str,File file) {
		// TODO Auto-generated method stub
		String nowlocal=file.getAbsolutePath();
		nowlocal=nowlocal.concat("/"+str);
	    file=new File(nowlocal);
		if(file.exists()) {
			System.out.println("mkdir:"+"无法创建文件或目录"+str+"已存在");
		}else {
		   file.mkdir();
		}
	}
}


pwd.java

package ezshell;

import java.io.File;

public class pwd {

	public void main(File file){
		// TODO Auto-generated method stub
		System.out.println("当前工作路径"+file.getAbsolutePath());		
	}
}


主目录

package ezshell;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Scanner; 
public class whole {
	public static HashMap<String,Integer> map;
	public static void connects() {
		map=new HashMap<String,Integer>();
		map.put("cat",1);
		map.put("cp",2);
		map.put("grep",3);
		map.put("echo",4);
		map.put("ls",5);
		map.put("mkdir",6);
		map.put("pwd",7);
		map.put("cd",8);
		map.put("exit",9);
	}
	public static Integer connect(String str) {
		connects();
		if(str.isEmpty()) return 0;
		if(map.containsKey(str)) {
			return map.get(str);
		}else {
			System.out.println(str+":未找到命令");
			return 0;
		}
	}
	public static void main(String[] args) throws IOException{
		boolean flag=true;
		File file=new File("/home/victor");
		while(flag) {
			System.out.print(file.getAbsolutePath()+":");
			Scanner sc=new Scanner(System.in);
			String str=sc.nextLine();
			String[] strs=str.split(" ");
			if(strs.length==1) {
				switch(connect(strs[0])) {
				case 5:
					ls l=new ls();
					l.main(file);
					break;
				case 7:
					pwd p=new pwd();
					p.main(file);
					break;
				case 9:
					flag=false;
					sc.close();
					break;
				}
			}
			else if(strs.length==2) {
				switch(connect(strs[0])) {
				case 1:
					cat c=new cat();
					c.main(strs[1],file);
					break;
				case 4:
					echo e=new echo();
					e.main(strs[1]);
					break;
				case 6:
					mkdir m=new mkdir();
					m.main(strs[1],file);
					break;
				case 8:
					cd d=new cd();
					file=d.main(strs[1],file);
					break;
				}
			}else if(strs.length==3) {
				switch(connect(strs[0])) {
				case 3:
					grep g=new grep();
					g.main(strs[1], strs[2],file);
					break;
				case 2:
					cp c=new cp();
					c.main(strs[1], strs[2],file);
					break;
				}
			}else if(strs.length==4) {
				switch(connect(strs[0])) {
				case 4:
					if(strs[2].equals("|grep")) {
						echogrep eg=new echogrep();
						eg.main(strs[1], strs[3]);
					}else {
						System.out.println("Commond not found");
					}
					break;
				}
			}else {
				System.out.println("Commond "+strs[0]+" not found");
				break;
			}
		}	
	} 
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值