J2SE5.0新特性之ProcessBuilder
来源:互联网 发布:淘宝淘客助手插件 编辑:程序博客网 时间:2024/05/16 09:42
这个例子使用了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);
}
}
}
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
- J2SE5.0新特性之ProcessBuilder
- J2SE5.0的新特性:ProcessBuilder
- J2SE5.0新特性
- J2SE5.0新特性之枚举类型
- J2SE5.0新特性之Foreach
- J2SE5.0新特性之静态导入
- J2SE5.0新特性之元数据
- J2SE5.0新特性之使用代理服务器
- J2SE5.0新特性之元数据
- J2SE5.0新特性之静态导入
- J2SE5.0新特性之Foreach
- J2SE5.0新特性之枚举类型
- J2SE5.0新特性之枚举类型
- J2SE5.0新特性之元数据
- J2SE5.0新特性之静态导入
- J2SE5.0新特性之Foreach
- J2SE5.0新特性之枚举类型
- 鼠标经过链接时,文字提示显示在页面的右下角,不会阻碍浏览者的视线
- struts1.1中的异常应用实践
- 一、无法打开ASP.NET Web项目
- struts框架中多行表单提交
- InfoHelper 项目散记(1)
- J2SE5.0新特性之ProcessBuilder
- InfoHelper 项目散记 (2)--项目启动会刚开完就出现了问题......
- 性能优化:==代替equals
- 转贴"北京出租车司机妙语录(标准的北京话)"
- 新一代Java技术即将出现
- CPP的未来(主要与JAVA的比较)
- 25句话
- 现在几乎是个网站就开始GMail....
- 不能容忍