Chinaunix首页 | 论坛 | 博客
  • 博客访问: 369632
  • 博文数量: 100
  • 博客积分: 2586
  • 博客等级: 少校
  • 技术积分: 829
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-09 15:20
个人简介

我是一个Java爱好者

文章分类

全部博文(100)

文章存档

2014年(2)

2013年(7)

2012年(2)

2010年(44)

2009年(28)

2008年(17)

我的朋友

分类: Java

2013-07-04 23:21:18

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

 

/**

 * J2SE5.0之前使用Runtime的exec方法执行本地命令.

 * J2Se5.0之后,可以使用ProcessBuilder执行本地命令

 * 它提供的功能更加丰富,能够设置设置工作目录、环境变量等

 * 本例PorcessBuilder执行Windows操作系统的"ipconfig/all"命令,获取本机网卡的MAC地址

*/

/**关键技术剖析

 * 用本命令名和命令的参数选项构造ProcessBuilder对象,它的start方法执行命令,启动一个进程,返回一个Process对象

 * ProcessBuilderenvironment方法获得运行进程的环境变量,得到一个Map,可以修改环境变量

 * ProcessBuilderdirectory方法切换工作目录

 * ProcessgetInputStream方法获得进程的标准输出流,getErrorStream方法获得进程的错误输出流

*/

public class UsingProcessBuilder {

       /**获取Windows系统下的网卡的MAC地址*/

       public static List getPhysicalAddress(){

              Process p = null;

              List address = new ArrayList(); //物理网卡列表

              try{

                     p = new ProcessBuilder("ipconfig","/all").start(); //执行ipconfig/all命令

              }catch(IOException e){

                     return address;

              }

              byte[] b = new byte[1024];

              int readbytes = -1;

              StringBuffer sb = new StringBuffer();

              //读取进程输出值

              //JAVA IO,输入输出是针对JVM而言,读写是针对外部数据源而言

              InputStream in = p.getInputStream();

              try{

                     while((readbytes = in.read(b)) != -1){

                            sb.append(new String(b,0,readbytes));

                     }

              }catch(IOException e1){

              }finally {

                     try{

                            in.close();

                     }catch (IOException e2){

                     }

              }

              //以下是分析输出值,得到物理网卡

              String rtValue = sb.toString();

              int i = rtValue.indexOf("Physical Address. . . . . . . . . :");

              while (i > 0){

                     rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());

                     address.add(rtValue.substring(1,18));

                     i = rtValue.indexOf("Physical Address. . . . . . . . . :");

              }

              return address;

       }

       /**执行自定义的一个命令,该命令放在C:/temp,并且需要两个环境变量的支持*/

       public static boolean executeMyCommand1(){

              //创建系统进程创建器

              ProcessBuilder pb = new ProcessBuilder("myCommand","myArg1","myArg2");

              Map env = pb.environment(); //获得进程的环境

              //设置和去除环境变量

              env.put("VAR1""myValue");

              env.remove("VAR0");

              env.put("VAR2", env.get("VAR1") + ";");

              //迭代环境变量,获取属性名和属性值

              Iterator it=env.keySet().iterator();

              String sysatt = null;

              while(it.hasNext())

              {

                     sysatt = (String)it.next();

                     System.out.println("System Attribute:"+sysatt+"="+env.get(sysatt));

              }

              pb.directory(new File("C:/temp"));

              try{

                     Process p = pb.start(); //得到进程实例

                     //等待进程执行完毕

                     if(p.waitFor() != 0){

                            //如果进程运行结果不为0,表示进程是错误退出的

                            //获得进程实例的错误输出

                            InputStream error = p.getErrorStream();

                            //do something

                     }

                     InputStream sdin = p.getInputStream(); //获得进程实例的标准输出

                     //do something

              }catch(IOException e){

              }catch(InterruptedException e){

              }

              return true;

       }

       public static void executeMyCommand2(){

              ProcessBuilder pb = null;

              String sysatt = null;

              try

        {

            //创建一个进程示例

            pb = new ProcessBuilder("cmd.exe");

            //获取系统参数并打印显示

            Map env = pb.environment();

            Iterator it=env.keySet().iterator();

            while(it.hasNext())

            {

                 sysatt = (String)it.next();

                System.out.println("System Attribute:"+sysatt+"="+env.get(sysatt));

            }

            //设置工作目录

            pb.directory(new File("d://myDir"));

            Process p = pb.start();

            //将要执行的Windows命令写入

            BufferedWriter bw=new BufferedWriter(newOutputStreamWriter(p.getOutputStream()));

            //'/r/n'是必须写入的     

            bw.write("test.bat /r/n");

            bw.write("ping -t /r/n");

            //flush()方法是必须调用的

            bw.flush();

            //将执行结果打印显示

            InputStream is = p.getInputStream();

            InputStreamReader isr = new InputStreamReader(is, "GBK");

            BufferedReader br = new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) != null)

            {

                System.out.println(line);

            }

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

       }

       public static void main(String[] args){

              List address = UsingProcessBuilder.getPhysicalAddress();

              for(String add : address){

                     System.out.printf("物理网卡地址: %s%n",add);

              }

              executeMyCommand1();

              executeMyCommand2();

       }

}

阅读(10905) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~