六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

写了个java版的cmd

[复制链接]

升级  20%

2

主题

2

主题

2

主题

童生

Rank: 1

积分
10
 楼主| 发表于 2013-2-6 08:41:36 | 显示全部楼层 |阅读模式
今天试着写了个java版的cmd,其实核心代码就是用RunTime类的exec()方法,不过觉得目录转换的模拟还是很有趣,“cd..”命令我还没写好.



代码如下:


JavaCmd.java
package com.cmd;import java.io.*;/** * 模拟cmd的类 * @author lupin * @version 1.0 2009-12-25 */public class JavaCmd {    public static Process execCmd(String command){                       //执行cmd命令的方法    JavaCmd.changDir(command);    Runtime run = Runtime.getRuntime();    Process pro =null;    try {pro = run.exec("cmd /c" + command,null,new File(System.getProperty("user.dir")));} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return pro;    }        public static void showResult(Process pro){                        //回显命令执行结果的方法InputStream is = pro.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is));String s = null;try {s = br.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}    while(s != null){    System.out.println(s);    try {s = br.readLine();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}    }    }        public static void showWindow(){                         //显示cmd窗口    System.out.println("Microsoft Windows XP [版本 5.1.2600]");    System.out.println("<c> 版权所有 1985-2001 Microsoft Corp.");    String strHome = System.getProperty("user.home");    System.setProperty("user.dir", strHome);    String strDir = System.getProperty("user.dir");    System.out.print(strDir + ">");    }        public static void changDir(String str){                              //转换目录的方法        if(str.indexOf("dir") < 0 && str.endsWith(":")){        System.setProperty("user.dir", str + "\\");        }        else if(str.indexOf("cd") >= 0 && str.indexOf(":") >= 0){        int i = 3;        String str1 = str.substring(i);        System.setProperty("user.dir",str1);        }        else if(str.indexOf("cd") >= 0 && str.indexOf(":") < 0){        String dir = System.getProperty("user.dir");        String temp = dir.substring(0,2);        String tempStr = str.substring(3);        System.setProperty("user.dir", temp + tempStr);        }        else if(str.indexOf("cd /") == 0){        String dir = System.getProperty("user.dir");        String dirTmp = dir.substring(0, 3);        System.setProperty("user.dir", dirTmp);        }        else if(str.indexOf("cd..") == 0){                }    }}


StartCmd.java

package com;import java.util.Scanner;import com.cmd.*;public class StartCmd {/** * @param args */public static void main(String[] args) {Scanner input = new Scanner(System.in);JavaCmd.showWindow();        while(true){    String command =input.nextLine();    Process pro = JavaCmd.execCmd(command);    JavaCmd.showResult(pro);    String strDir = System.getProperty("user.dir");    System.out.print(strDir + ">");        }}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表