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