03 模拟命令行

Posted alichengxuyuan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了03 模拟命令行相关的知识,希望对你有一定的参考价值。

1 按照下图要求完成自己的命令行执行

技术图片


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;

public class 命令模拟 {
	static String basePath = "C:/Users/zhangli/Desktop";

	public static void main(String[] args) {

		try {
			basePath = args[0];
		} catch (Exception e) {
			// System.out.println("没有命令行参数,所以取初始值:C:/Users/zhangli");
		}
		Scanner in = new Scanner(System.in);
		while (true) {

			System.out.print(basePath + ">");

			String cmd = in.nextLine();
			switch (cmd) {
			case "mytime":
				System.out.println(fetchCurrentTime());
				break;
			case "mydir":
				System.out.println(showDirInfos());
				break;
			case "mytree":
				printTree(new File(basePath), 0);
				break;
			case "myhelp":
				System.out.println(fetchCmdHelpInfo());
				break;
			case "quit":
				System.exit(0);
				break;
			case "exit":
				System.exit(0);
				break;
			default:
				if (cmd.startsWith("mycd")) {
					changeDirectory(cmd);
				} else if (cmd.startsWith("mycalendar")) {
					System.out.println(showCalendar(cmd));
				} else if (cmd.startsWith("myremove")) {
					delete(cmd);
				} else {
					System.out.println("非法命令或者请求,请重新输入!");
				}
				break;
			}
		}

	}

	private static String showCalendar(String cmd) {
		StringBuffer sb = new StringBuffer(64);
		String[] cs = cmd.split("s+");
		if (cs.length == 1) {
			int year = Calendar.getInstance().get(Calendar.YEAR);
			int month = Calendar.getInstance().get(Calendar.MONTH) + 1;
			return year + "年" + month + "月
"
					+ printCalendarThisMonth(year, month);
		} else if (cs.length == 2) {
			int year = Integer.parseInt(cs[1]);
			for (int i = 1; i <= 12; i++) {
				sb.append(year + "年" + i + "月
"
						+ printCalendarThisMonth(year, i));
			}
			return sb.toString();
		} else if (cs.length == 3) {
			int year = Integer.parseInt(cs[1]);
			int month = Integer.parseInt(cs[2]);
			return year + "年" + month + "月
"
					+ printCalendarThisMonth(year, month);
		}
		return "输入参数个数或者类型错误,请重新输入!";
	}

	private static String showDirInfos() {
		StringBuffer sb = new StringBuffer(32);
		File f = new File(basePath);
		File[] fs = f.listFiles();
		sb.append(
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(f
						.lastModified()))).append("	<DIR>	").append(".
");
		sb.append(
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(f
						.lastModified()))).append("	<DIR>	").append("..
");
		for (int i = 0; i < fs.length; i++) {
			if (!fs[i].isHidden()) {
				sb.append(
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date(fs[i].lastModified())))
						.append("	");
				sb.append(fs[i].isDirectory() ? "<DIR>	" : "	");
				sb.append(fs[i].getName()).append("
");
			}
		}
		return sb.toString();
	}

	private static void changeDirectory(String cmd) {
		String[] cmds = cmd.split("s+");
		if (cmds.length == 1) {
			return;
		} else {
			String destPath = cmds[1];
			if (".".equals(destPath)) {

			} else if ("..".equals(destPath)) {
				String[] paths = basePath.split("/");
				StringBuffer temp = new StringBuffer(32);
				for (int i = 0; i < paths.length - 1; i++) {
					temp.append(paths[i]).append("/");
				}
				basePath = temp.substring(0, temp.length() - 1).toString();
			} else {
				// 如果给出的是相对路径
				String temp = basePath + "/" + destPath;
				if (new File(temp).exists()) {
					basePath = basePath + "/" + destPath;
				} else if (new File(destPath).exists()) {
					basePath = destPath;
				} else {
					System.out.println("非法路径,请重新输入!");
				}
			}
		}

	}

	private static String fetchCmdHelpInfo() {
		StringBuffer sb = new StringBuffer(64);

		// System.out.println("	 mytime 获取当前时间");
		sb.append("命令提示信息如下:
");
		sb.append("	 mytime 获取当前时间
");
		sb.append("	 myhelp 获取命令提示
");
		sb.append("	 mycd 切换目录
");
		sb.append("	 mycalendar 打印日历
");
		sb.append("	 mydir 显示目录信息
");
		sb.append("	 mytree 显示目录所有文件信息
");
		sb.append("	 exit/quit 退出当前程序
");
		return sb.toString();
	}

	private static String fetchCurrentTime() {
		String ret = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒")
				.format(new Date());
		return ret;
	}

	public static String printCalendarThisMonth(int year, int month) {
		StringBuffer sb = new StringBuffer(64);
		int[] days = new int[42];

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		int dayNuminThisMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		int firstDay = cal.get(Calendar.DAY_OF_WEEK);
		int day = 1;
		for (int i = firstDay - 1; i < dayNuminThisMonth + firstDay - 1; i++) {
			days[i] = day++;
		}
		sb.append("天	一	二	三	四	五	六
");
		for (int i = 0; i < days.length; i++) {
			if (days[i] == 0) {
				sb.append("	");
			} else {
				sb.append(days[i] + "	");
			}
			if ((i + 1) % 7 == 0) {
				sb.append("
");
			}
		}
		return sb.toString();
	}

	public static void printTree(File f, int level) {

		System.out.println(printStr("	", level) + f.getName());
		File[] fs = null;
		if (f.isDirectory() && ((fs = f.listFiles()) != null)) {
			for (int i = 0; i < fs.length; i++) {
				printTree(fs[i], level + 1);
			}
		}
	}
	
	public static void delete(String cmd)
	{
		String[] cmds = cmd.split("s+");
		if(cmds.length==1)
		{
			System.out.println("请输入当前路径下所要删除的文件名");
		}
		else if(cmds.length==2)
		{
			if(new File(basePath+"/"+cmds[1]).exists())
			{
				removeDir(new File(basePath+"/"+cmds[1]));
			}
			else
			{
				System.out.println("请输入当前路径下所要删除的文件名");
			}
		}
	}

	public static void removeDir(File f) {
		if (f.isDirectory()) {
			File[] fs = f.listFiles();
			for (int i = 0; i < fs.length; i++) {
				removeDir(fs[i]);
			}
		}
		f.delete();
	}

	public static String printStr(String str, int count) {
		StringBuffer sb = new StringBuffer(32);
		for (int i = 0; i < count; i++) {
			sb.append(str);
		}

		return sb.toString();
	}
}

以上是关于03 模拟命令行的主要内容,如果未能解决你的问题,请参考以下文章

sql [SQL查询片段]用于在命令行或通过R和其他工具使用SQL的快速代码段#tags:sql,R,text processing,命令li

如何管理在每个 git 版本中添加私有代码片段?

iOS Swift 中的 Android 片段模拟

如何使用命令行在 Windows Mobile 模拟器中重建 .net CF 应用程序和部署

行历史查看器 - Git

命令行模拟进度条