中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > JDK核心API
J2SE5.0新特性之ProcessBuilder
作者:未知 时间:2005-07-24 21:14 出处:JR 责编:chinaitpower
              摘要:J2SE5.0新特性之ProcessBuilder
这个例子使用了J2SE5.0的ProcessBuilder类执行外部的程序,相对于 Runtime.exec ,它更方便,可以设置环境变量等。这里使用它在windows下读取物理网卡的地址
  1. package com.kuaff.jdk5package;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class ProcessBuilderShow
  7. {
  8.     public static List<String> getPhysicalAddress()
  9.     {
  10.         Process p = null;
  11.         //物理网卡列表
  12.         List<String> address = new ArrayList<String>();
  13.         try
  14.         {
  15.             //执行ipconfig /all命令
  16.             p = new ProcessBuilder("ipconfig""/all").start();
  17.         }
  18.         catch (IOException e)
  19.         {
  20.             return  address;
  21.         }
  22.         byte[] b = new byte[1024];
  23.         StringBuffer sb = new StringBuffer();
  24.         //读取进程输出值
  25.         InputStream in = p.getInputStream();
  26.         try
  27.         {
  28.             while (in.read(b)>0)
  29.             {
  30.                 sb.append(new String(b));
  31.             }
  32.         }
  33.         catch (IOException e1)
  34.         {
  35.         }
  36.         finally
  37.         {
  38.             try
  39.             {
  40.                 in.close();
  41.             }
  42.             catch (IOException e2)
  43.             {
  44.             }
  45.         }
  46.         //以下分析输出值,得到物理网卡
  47.         String rtValue = sb.substring(0);
  48.         int i = rtValue.indexOf("Physical Address. . . . . . . . . :");
  49.         while(i>0)
  50.         {
  51.             rtValue = rtValue.substring(i + "Physical Address. . . . . . . . . :".length());
  52.             address.add(rtValue.substring(0,18));
  53.             i = rtValue.indexOf("Physical Address. . . . . . . . . :");
  54.         }
  55.         
  56.         return address;
  57.         
  58.     }
  59.     public static void main(String[] args)
  60.     {
  61.         List<String> address = ProcessBuilderShow.getPhysicalAddress();
  62.         for(String add:address)
  63.         {
  64.             System.out.printf("物理网卡地址:%s%n", add);
  65.         }
  66.     }
  67. }


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有