服务热线:13616026886

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

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

j2se新特性 windows下读取网卡物理地址

个例子使用了j2se5.0的processbuilder类执行外部的程序,相对于 runtime.exec ,它更方便,可以设置环境变量等。

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<string> getphysicaladdress()
    {
        process p = null;
        //物理网卡列表
        list<string> address = new arraylist<string>();

        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<string> address = processbuildershow.getphysicaladdress();
        for(string add:address)
        {
            system.out.printf("物理网卡地址:%s%n", add);
        }
    }
}

(t007)


扫描关注微信公众号