⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydos.java

📁 采用java语言编写的
💻 JAVA
字号:
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.IOException;
class  Dos{
	static String Pathname=System.getProperty("user.home");//定义一个全局变Pathname,作为当前目录
	public void ShowPathname(){//显示命令行提示符函数
		System.out.print(Pathname+'>');
	}
	public void EnterRoot(String root){//实现盘符切换,进入根目录
		Pathname=root+"\\";//将根目录的名字作为当前路径
		System.out.print(Pathname+'>');//打印根目录,表示进入根目录
	}
	public void cd(String Directory){//进入指定的子目录,实现cd功能
		String temp;//当前路径名
		if(Pathname.endsWith("\\"))
			temp=Pathname+Directory;//当前路径由之前的路径和子目录的名字构成
		else
			temp=Pathname+"\\"+Directory;//当前路径由之前的路径和子目录的名字构成
		boolean IsDirectory;//标识子目录是否存在		
		File directory=new File(temp);
		IsDirectory=directory.exists();//看看是否存在指定的子目录
		if(IsDirectory){//如果存在
			Pathname=temp;//则将当前临时存放路径的变量的值赋给路径名
			this.ShowPathname();//显示路径,表示已经进入该子目录
		}
		else 
			System.out.println("系统找不到指定的路径");//如果不存在,则打印提示信息	
	}
	public void dir(){//实现dir的功能,列出该目录下的所有子目录和文件,及它们的一些属性
		System.out.println(Pathname+"的目录");
		File file=new File(Pathname);
		File[] fl=file.listFiles();//得到该目录下所有子目录和普通文件的列表
		int DirectoryNum=0,FileNum=0;//记录文件夹和文件的个数
		long len=0;//记录该目录下所有文件的总字节数
		for(int i=0;i<fl.length;i++){
			long time=fl[i].lastModified();//得到第i个文件最后一次被修改的时间
			if(fl[i].isDirectory()){//如果第i个是文件夹
				System.out.println(time+"\t"+"<DIR>"+"\t"+fl[i].getName());//打印信息
				DirectoryNum++;//文件夹的个数加1
			}
			if(fl[i].isFile()){//如果第i个是普通文件
				len=len+fl[i].length();//文件的总字节数增加
				System.out.println(time+"\t"+fl[i].length()+"\t"+fl[i].getName());//打印信息
				FileNum++;//文件个数加1
			}			
		}
		System.out.println("\t"+FileNum+"个文件"+len+"个字节");//打印出该目录下总文件数和总字节数
		System.out.println("\t"+DirectoryNum+"个目录");//打印该目录下子目录的总是
		this.ShowPathname();
	}
	public void mkdir(String Directory){//实现mkdir的功能,及创建子目录的功能
		String path;//要创建目录的路径名
		boolean sgn;//标识是否创建成功
		if(Pathname.endsWith("\\"))
			path=Pathname;
		else path=Pathname+"\\";
		File file=new File(path,Directory);//创建一个File类的对象
		sgn=file.mkdir();//创建一个目录		
		if(sgn)
			System.out.println("成功创建子目录");
		else
		    System.out.println("创建子目录失败了");
		this.ShowPathname();//显示路径		
	}
	public void chdir(){//显示当前路径名
		System.out.println(Pathname);
		System.out.println("");
		this.ShowPathname();
	}
	public void rmdir(String Directory){//实现rmdir的功能,及删除子目录
		String path;//要删除目录的路径名
		boolean sgn;//标识是否删除成功
		if(Pathname.endsWith("\\"))
			path=Pathname;
		else path=Pathname+"\\";;
		File file=new File(path,Directory);//创建一个File类的对象
		if(file.exists()){//如果存在该目录
		sgn=file.delete();//删除一个目录
		if(sgn)
			System.out.println("成功删除子目录");
		else
		    System.out.println("删除子目录失败了");
		this.ShowPathname();//显示路径
		}
		else {//如果不存在该目录
			System.out.println("该文件不在该目录中");
			this.ShowPathname();//显示路径
		}
	}
	public void back(){//返回上级目录,实现cd..的功能
		if(Pathname.endsWith("\\"))//如果当前路径以\结尾,则说明已在根目录
			System.out.println("已经到了根目录,无法再返回上级目录了");
		else {//否则
			int sgn=0;
			sgn=Pathname.lastIndexOf("\\");//得到最后一个\的下标
			Pathname=Pathname.substring(0,sgn+1);//提取上级目录的路径名
		}
		this.ShowPathname();
	}

	public void help(){
		System.out.println("C:,D:,E:,F:,G:"+'\t'+"//盘符切换,进入指定驱动器");	
		System.out.println("cd"+'\t'+"//进入指定子目录");	
		System.out.println("dir"+'\t'+"//显示当前目录下的文件和目录信息");
		System.out.println("mkdir  文件夹名字"+'\t'+"//创建子目录");
		System.out.println("chdir"+'\t'+"//显示当前路径");
		System.out.println("rmdir  文件夹名字"+'\t'+"//删除指定目录");
		System.out.println("cd.."+'\t'+"//返回上级目录");;
		System.out.println("exit"+'\t'+"//退出程序");
		this.ShowPathname();
	}
	public void exit(){
		System.exit(0);
	}	
}
public class MyDos {
	public static void main(String[] args)throws IOException {
		System.out.println("您可以使用help命令,查看如何使用指令");
		System.out.println(System.getProperty("os.name")+'\t'+"版本"+System.getProperty("os.version"));
		Dos mydos=new Dos();
		mydos.ShowPathname();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String line = "";
		while(true){
			int m=0;
			line=br.readLine();//读取输入的命令
			line=line.trim();
			String[]  str=line.split(" ");//对命令进行拆分
			String[]  s=new String[2];
			for(int i=0;i<str.length;i++){
				if(!str[i].equals("")){
					s[m]=str[i];
					m++;
				}
			}
			//以下函数根据不同的命令调用不同的函数,完成特定功能
			if(str.length<2)
				s[1]="";
			if(s[0].length()==2 && s[0].charAt(1)==':' ){
				mydos.EnterRoot(s[0]);
			}
			else if(s[0].equalsIgnoreCase("cd")){
				if(s[1].length()==0){
					System.out.println("不能缺省目录的名字");
					mydos.ShowPathname();
				}
				else
					mydos.cd(s[1]);
			}
			else if(s[0].equalsIgnoreCase("dir")){
				mydos.dir();
			}
			
			else if(s[0].equalsIgnoreCase("mkdir")){
				if(s[1].length()==0){
					System.out.println("不能缺省目录的名字");
					mydos.ShowPathname();
				}
				else
				mydos.mkdir(s[1]);
			}
			else if(s[0].equalsIgnoreCase("chdir")){
				mydos.chdir();
			}
			else if(s[0].equalsIgnoreCase("rmdir")){
				if(s[1].length()==0){
					System.out.println("不能缺省目录的名字");
					mydos.ShowPathname();
				}
				else
				mydos.rmdir(s[1]);
			}
			else if(s[0].equalsIgnoreCase("cd..")){
				mydos.back();
			}			
			else if(s[0].equalsIgnoreCase("help")){
				mydos.help();
			}
			else if(s[0].equalsIgnoreCase("exit")){
				mydos.exit();
			}
			else {
				System.out.println(s[0]+"不是内部或外部命令,也不是可运行的程序或批处理文件。");
				mydos.ShowPathname();
			}
		}	
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -