This example uses the j2se5.0 Processbuilder class to perform an external program, which is more convenient than the runtime.exec and can set environment variables. 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;
Physical network card list
list<string> address = new arraylist<string> ();
Try
{
Execute Ipconfig/all command
p = new Processbuilder ("ipconfig", "/all"). Start ();
}
catch (IOException E)
{
return address;
}
Byte[] B = new byte[1024];
StringBuffer sb = new StringBuffer ();
Reading process output values
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)
{
}
}
The following analysis outputs the value, obtains the physical network card
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 ("Physical network card address:%s%n", add);
}
}
}