J2SE5.0的ProcessBuilder特点[Java编程]
本文“J2SE5.0的ProcessBuilder特点[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
这个例子利用了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<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);
}
}
}
以上是“J2SE5.0的ProcessBuilder特点[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |