java获得MAC地址

来源:互联网 发布:权威的数据统计网站 编辑:程序博客网 时间:2024/05/22 10:44

import java.io.*;

            import java.util.regex.*;

            public class ReadMacByJava

            {

            private String mPhysicalAddress = "";

            private int mPhysicalMacNumber = 0;

            private boolean isInit = false;

            

            public ReadMacByJava()

            {

            }

            

            public void init()

            {

            try

            {

            String line;

            Process process = Runtime.getRuntime().exec("cmd /c ipconfig /all");

            BufferedReader bufferedReader = new BufferedReader(

            new InputStreamReader(process.getInputStream()));

            Pattern macPattern =

            Pattern.compile("([0-9A-Fa-f]{2})(-[0-9A-Fa-f]{2}){5}");

            Matcher macMatcher;

            boolean result;

            while ((line = bufferedReader.readLine()) != null)

            {

            if ("".equals(line))

            continue;

            macMatcher = macPattern.matcher(line);

            result = macMatcher.find();

            if (result)

            {

            mPhysicalMacNumber++;

            if ("".equals(mPhysicalAddress))

            mPhysicalAddress = macMatcher.group(0);

            else

            mPhysicalAddress += ("," + macMatcher.group(0));

            }

            }

            }

            catch (Exception e)

            {

            e.printStackTrace();

            }

            isInit = true;

            }

            public String getPhysicalAddress()

            {

            if (isInit)

            return this.mPhysicalAddress;

            else

            return "Mac is not init.";

            }

            public int getPhysicalMacNumber()

            {

            if (isInit)

            return this.mPhysicalMacNumber;

            else

            {

            System.out.println("Mac is not init.");

            return 0;

            }

            }

            public static void main(String[] args)

            {

            ReadMacByJava mac = new ReadMacByJava();

            mac.init();

            System.out.println("MAC "+mac.getPhysicalMacNumber()+" address :"

            + mac.getPhysicalAddress());

            }

            }