服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

j2se5.0新特性之processbuilder


  这个例子使用了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);
  }
  }
  }

扫描关注微信公众号