六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 64|回复: 0

java 获取操作系统收集(2)

[复制链接]

升级  15.33%

19

主题

19

主题

19

主题

秀才

Rank: 2

积分
73
 楼主| 发表于 2013-2-7 03:54:06 | 显示全部楼层 |阅读模式
1.package com.cmd;   
2.  
3.import java.io.*;   
4.  
5.  
6./**  
7. * 模拟cmd的类  
8. * @author lupin  
9. * @version 1.0 2009-12-25  
10. */  
11.public class JavaCmd {   
12.    public static Process execCmd(String command){                       //执行cmd命令的方法   
13.        JavaCmd.changDir(command);   
14.        Runtime run = Runtime.getRuntime();   
15.        Process pro =null;   
16.        try {   
17.            pro = run.exec("cmd /c" + command,null,new File(System.getProperty("user.dir")));   
18.        } catch (IOException e) {   
19.            // TODO Auto-generated catch block   
20.            e.printStackTrace();   
21.        }   
22.        return pro;   
23.    }   
24.      
25.    public static void showResult(Process pro){                        //回显命令执行结果的方法   
26.        InputStream is = pro.getInputStream();   
27.        BufferedReader br = new BufferedReader(new InputStreamReader(is));   
28.        String s = null;   
29.        try {   
30.            s = br.readLine();   
31.        } catch (IOException e) {   
32.            // TODO Auto-generated catch block   
33.            e.printStackTrace();   
34.        }   
35.        while(s != null){   
36.            System.out.println(s);   
37.            try {   
38.                s = br.readLine();   
39.            } catch (IOException e) {   
40.                // TODO Auto-generated catch block   
41.                e.printStackTrace();   
42.            }   
43.        }   
44.    }   
45.      
46.    public static void showWindow(){                         //显示cmd窗口   
47.        System.out.println("Microsoft Windows XP [版本 5.1.2600]");   
48.        System.out.println("<c> 版权所有 1985-2001 Microsoft  Corp.");   
49.        String strHome = System.getProperty("user.home");   
50.        System.setProperty("user.dir", strHome);   
51.        String strDir = System.getProperty("user.dir");   
52.        System.out.print(strDir + ">");   
53.    }   
54.      
55.    public static void changDir(String str){                              //转换目录的方法   
56.        if(str.indexOf("dir") < 0 && str.endsWith(":")){   
57.            System.setProperty("user.dir", str + "\\");   
58.        }   
59.        else if(str.indexOf("cd") >= 0 && str.indexOf(":") >= 0){   
60.            int i = 3;   
61.            String str1 = str.substring(i);   
62.            System.setProperty("user.dir",str1);   
63.        }   
64.        else if(str.indexOf("cd") >= 0 && str.indexOf(":") < 0){   
65.            String dir = System.getProperty("user.dir");   
66.            String temp = dir.substring(0,2);   
67.            String tempStr = str.substring(3);   
68.            System.setProperty("user.dir", temp + tempStr);   
69.        }   
70.        else if(str.indexOf("cd /") == 0){   
71.            String dir = System.getProperty("user.dir");   
72.            String dirTmp = dir.substring(0, 3);   
73.            System.setProperty("user.dir", dirTmp);   
74.        }   
75.        else if(str.indexOf("cd..") == 0){   
76.               
77.        }   
78.    }   
79.}  
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 block
e.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 block
e.printStackTrace();
}
    while(s != null){
    System.out.println(s);
    try {
s = br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.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


Java代码
1.package com;   
2.  
3.import java.util.Scanner;   
4.  
5.import com.cmd.*;   
6.  
7.public class StartCmd {   
8.  
9.    /**  
10.     * @param args  
11.     */  
12.    public static void main(String[] args) {   
13.        Scanner input = new Scanner(System.in);   
14.        JavaCmd.showWindow();   
15.        while(true){   
16.            String command =input.nextLine();   
17.            Process pro = JavaCmd.execCmd(command);   
18.            JavaCmd.showResult(pro);   
19.            String strDir = System.getProperty("user.dir");   
20.            System.out.print(strDir + ">");   
21.        }   
22.    }   
23.  
24.}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

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