Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26267545
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Java

2010-03-28 23:33:42

package cn.ty.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SysUtils {
    /**
    * FunName:           executeSys
    * Description :      执行系统命令并返回一段字符串
    * @param:           系统命令:String
    * @return String:   返回执行的结果集
    * @Create Date:      2010-03-25
    */
    public static byte[] executeSy(String command) {
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
            p = r.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
        StringBuffer sb=new StringBuffer();
        String inline;
        try {
            while(null!=(inline=br.readLine())){
                sb.append(inline).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String reuslts = sb.toString();
        byte[] bytes = reuslts.getBytes();
        return bytes;
    }
    
    /**
    * FunName:           executeSysString
    * Description :      执行系统命令并返回一段字符串
    * @param:           系统命令:String
    * @return String:   返回执行的结果集
    * @Create Date:      2010-03-25
    */
    public static String executeSysString(String command) {
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
            p = r.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
        StringBuffer sb=new StringBuffer();
        String inline;
        try {
            //注意:一般如果我们调用ifconfig命令的话是会有空行的。这里我们通过
//inline.length()的技巧解决了这个问题。成功将空行去掉。记号一下!
            while(null!=(inline=br.readLine())){
                if(inline.length() > 0) {
                    sb.append(inline).append("\n");
                    continue;
                } else {
                    continue;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        String reuslts = sb.toString();
        return reuslts;
    }
   
   
    /**
    * FunName:           executeSys
    * Description :      执行系统命令
    * @param:           系统命令:String
    * @Create Date:      2010-03-25
    */
    public static void executeCommand(String command) {
        System.out.println(command);
        Runtime r = Runtime.getRuntime();
        Process p = null;
        try {
            p = r.exec(command);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        String out = SysUtils.executeSysString("ipconfig");
        System.out.println(out);
        //SysUtils.executeCommand("kill -9 3104");
    }
   
}
 
阅读(1315) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~