码迷,mamicode.com
首页 > 其他好文 > 详细

03 模拟命令行

时间:2020-03-29 18:00:09      阅读:50      评论:0      收藏:0      [点我收藏+]

标签:令行   load   count   top   substr   png   自己   file   while   

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 + "月\n"
					+ 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 + "月\n"
						+ 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 + "月\n"
					+ 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("\t<DIR>\t").append(".\n");
		sb.append(
				new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(f
						.lastModified()))).append("\t<DIR>\t").append("..\n");
		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("\t");
				sb.append(fs[i].isDirectory() ? "<DIR>\t" : "\t");
				sb.append(fs[i].getName()).append("\n");
			}
		}
		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("\t mytime 获取当前时间");
		sb.append("命令提示信息如下:\n");
		sb.append("\t mytime 获取当前时间\n");
		sb.append("\t myhelp 获取命令提示\n");
		sb.append("\t mycd 切换目录\n");
		sb.append("\t mycalendar 打印日历\n");
		sb.append("\t mydir 显示目录信息\n");
		sb.append("\t mytree 显示目录所有文件信息\n");
		sb.append("\t exit/quit 退出当前程序\n");
		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("天\t一\t二\t三\t四\t五\t六\n");
		for (int i = 0; i < days.length; i++) {
			if (days[i] == 0) {
				sb.append("\t");
			} else {
				sb.append(days[i] + "\t");
			}
			if ((i + 1) % 7 == 0) {
				sb.append("\n");
			}
		}
		return sb.toString();
	}

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

		System.out.println(printStr("\t", 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 模拟命令行

标签:令行   load   count   top   substr   png   自己   file   while   

原文地址:https://www.cnblogs.com/alichengxuyuan/p/12578006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!