icon您现在的位置: IT培训网 >> 学习园地 >> 软件教程 >> 正文

J2SE5.0新特性之ProcessBuilder

[来 源]不详[作 者]佚名 [发表时间] 2008-3-8 11:56:41     点击次数:
  这个例子使用了J2SE5.0的ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址

  package com.kuaff.jdk5package;

  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.List;

  public class ProcessBuilderShow
  {
  public static List getPhysicalAddress()
  {
  Process p = null;
  //物理网卡列表
  List address = new ArrayList();

  try
  {
   //执行ipconfig /all命令
   p = new ProcessBuilder("ipconfig", "/all").start();
  }
  catch (IOException e)
  {
   return address;
  }
  byte[] b = new byte[1024];
  StringBuffer sb = new StringBuffer();
  //读取进程输出值
  InputStream in = p.getInputStream();
  try
  {
   while (in.read(b)>0)
   {
    sb.append(new String(b));
   }
  }
  catch (IOException e1)
  {
  }
  finally
  {
   try
   {
    in.close();
   }
   catch (IOException e2)
   {
   }
  }
  //以下分析输出值,得到物理网卡
  String rtValue = sb.substring(0);
  int i = rtValue.indexOf("Physical Address. . . . . . . . . :");
  while(i>0)
  {
   rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());
   address.add(rtValue.substring(0,18));
   i = rtValue.indexOf("Physical Address. . . . . . . . . :");
  }
  return address;
  }
  public static void main(String[] args)
  {
  List address = ProcessBuilderShow.getPhysicalAddress();
  for(String add:address)
  {
   System.out.printf("物理网卡地址:%s%n", add);
  }
  }
  } <

J2SE5.0新特性之ProcessBuilder的文章由IT培训网编写收集整理,转载引用本网站的原创文章,请务必注明信息来源,标明“IT培训网”字样如果我们的文章有涉及或侵犯您的有关权益,请即时与我们联系, 注明网址及文章,我们会即时处理或删除, 感谢您的合作!

更多icon联系我们

    中山大学计算机学院
    :020-84113178(上班时间)

    :13556192292(下班时间)

       
    郑老师: 点击这里给我发消息
       
    曾老师: 点击这里给我发消息

更多icon免费公开课

最新文章

更多icon推荐文章
  • 此栏目下没有推荐文章