zoukankan      html  css  js  c++  java
  • 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. }
  • 相关阅读:
    ES2017 新特性:Async Functions (异步函数)
    为什么 window.location.search 为空?
    vue-cli 构建Vue项目后的打包和发布
    Vue.js父子组件如何传值 通俗易懂
    vue 面试题(文章末尾还有其他链接)
    vue组件命名和传值 and 父子组件传值
    line-gradient 之渐变角度
    vue-router路由模式
    vue-router 去掉#
    记录vue项目上线遇到的一些问题
  • 原文地址:https://www.cnblogs.com/Finding2013/p/159206.html
Copyright © 2011-2022 走看看