J2SE new feature Read network card Physical address under Windows

Source: Internet
Author: User
Tags return string stringbuffer
J2se|window

An 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 ();        Read process output value 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) {    }//below analysis output value, get physical nic 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 to 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); }    }}
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.